diff --git a/BUILD b/BUILD index 9913e9199a..ad85b202a9 100644 --- a/BUILD +++ b/BUILD @@ -9,7 +9,6 @@ load( "make_shell_script", "upb_amalgamation", ) - load( "//bazel:upb_proto_library.bzl", "upb_proto_library", @@ -33,7 +32,7 @@ CPPOPTS = [ COPTS = CPPOPTS + [ # copybara:strip_for_google3_begin "-pedantic", - "-std=c89", + "-Wstrict-prototypes", # copybara:strip_end ] @@ -43,6 +42,11 @@ config_setting( visibility = ["//visibility:public"], ) +config_setting( + name = "windows", + constraint_values = ["@bazel_tools//platforms:windows"], +) + config_setting( name = "fuzz", values = {"define": "fuzz=true"}, @@ -58,6 +62,7 @@ cc_library( "upb/generated_util.h", "upb/msg.c", "upb/msg.h", + "upb/port.c", "upb/port_def.inc", "upb/port_undef.inc", "upb/table.c", @@ -69,7 +74,10 @@ cc_library( "upb/encode.h", "upb/upb.h", ], - copts = COPTS, + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }), visibility = ["//visibility:public"], ) @@ -81,23 +89,26 @@ cc_library( # give up any backward compatibility guarantees. cc_library( name = "generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me", - textual_hdrs = [ - "upb/port_def.inc", - "upb/port_undef.inc", - ], hdrs = [ "upb/generated_util.h", "upb/msg.h", ], - deps = [":upb"], - copts = COPTS, + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }), + textual_hdrs = [ + "upb/port_def.inc", + "upb/port_undef.inc", + ], visibility = ["//visibility:public"], + deps = [":upb"], ) upb_proto_library( name = "descriptor_upbproto", - deps = ["@com_google_protobuf//:descriptor_proto"], visibility = ["//visibility:public"], + deps = ["@com_google_protobuf//:descriptor_proto"], ) cc_library( @@ -110,7 +121,10 @@ cc_library( "upb/def.h", "upb/msgfactory.h", ], - copts = COPTS, + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }), visibility = ["//visibility:public"], deps = [ ":descriptor_upbproto", @@ -135,7 +149,10 @@ cc_library( "upb/legacy_msg_reflection.c", ], hdrs = ["upb/legacy_msg_reflection.h"], - copts = COPTS, + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }), deps = [ ":table", ":upb", @@ -153,7 +170,10 @@ cc_library( "upb/handlers.h", "upb/sink.h", ], - copts = COPTS, + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }), deps = [ ":reflection", ":table", @@ -177,7 +197,10 @@ cc_library( "upb/pb/encoder.h", "upb/pb/textprinter.h", ], - copts = COPTS, + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }), deps = [ ":descriptor_upbproto", ":handlers", @@ -198,7 +221,10 @@ cc_library( "upb/json/parser.h", "upb/json/printer.h", ], - copts = COPTS, + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }), deps = [ ":upb", ":upb_pb", @@ -228,7 +254,10 @@ cc_library( "upbc/message_layout.h", ], hdrs = ["upbc/generator.h"], - copts = CPPOPTS, + copts = select({ + ":windows": [], + "//conditions:default": CPPOPTS + }), deps = [ "@absl//absl/base:core_headers", "@absl//absl/container:flat_hash_map", @@ -241,7 +270,10 @@ cc_library( cc_binary( name = "protoc-gen-upb", srcs = ["upbc/main.cc"], - copts = CPPOPTS, + copts = select({ + ":windows": [], + "//conditions:default": CPPOPTS + }), visibility = ["//visibility:public"], deps = [ ":upbc_generator", @@ -275,7 +307,10 @@ cc_library( "tests/test_util.h", "tests/upb_test.h", ], - copts = CPPOPTS, + copts = select({ + ":windows": [], + "//conditions:default": CPPOPTS + }), deps = [ ":handlers", ":upb", @@ -288,7 +323,10 @@ cc_test( "tests/pb/test_varint.c", "upb/pb/varint.int.h", ], - copts = COPTS, + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }), deps = [ ":upb", ":upb_pb", @@ -314,7 +352,10 @@ cc_test( "tests/pb/test_decoder.cc", "upb/pb/varint.int.h", ], - copts = CPPOPTS, + copts = select({ + ":windows": [], + "//conditions:default": CPPOPTS + }), deps = [ ":handlers", ":test_decoder_upbproto", @@ -339,7 +380,10 @@ upb_proto_reflection_library( cc_test( name = "test_cpp", srcs = ["tests/test_cpp.cc"], - copts = CPPOPTS, + copts = select({ + ":windows": [], + "//conditions:default": CPPOPTS + }), deps = [ ":handlers", ":reflection", @@ -353,7 +397,10 @@ cc_test( cc_test( name = "test_table", srcs = ["tests/test_table.cc"], - copts = CPPOPTS, + copts = select({ + ":windows": [], + "//conditions:default": CPPOPTS + }), deps = [ ":table", ":upb", @@ -363,12 +410,15 @@ cc_test( # OSS-Fuzz test cc_binary( - testonly = 1, name = "file_descriptor_parsenew_fuzzer", + testonly = 1, srcs = ["tests/file_descriptor_parsenew_fuzzer.cc"], - copts = CPPOPTS + select({ + copts = select({ + ":windows": [], + "//conditions:default": CPPOPTS + }) + select({ "//conditions:default": [], - ":fuzz": ["-fsanitizer=fuzzer,address"], + ":fuzz": ["-fsanitize=fuzzer,address"], }), defines = select({ "//conditions:default": [], @@ -389,7 +439,10 @@ upb_proto_reflection_library( cc_test( name = "test_encoder", srcs = ["tests/pb/test_encoder.cc"], - copts = CPPOPTS, + copts = select({ + ":windows": [], + "//conditions:default": CPPOPTS + }), deps = [ ":descriptor_upbproto", ":descriptor_upbreflection", @@ -431,7 +484,10 @@ cc_test( srcs = [ "tests/json/test_json.cc", ], - copts = CPPOPTS, + copts = select({ + ":windows": [], + "//conditions:default": CPPOPTS + }), deps = [ ":test_json_upbproto", ":test_json_upbprotoreflection", @@ -459,7 +515,10 @@ cc_binary( srcs = [ "tests/conformance_upb.c", ], - copts = COPTS + ["-Ibazel-out/k8-fastbuild/bin"], + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }) + ["-Ibazel-out/k8-fastbuild/bin"], deps = [ ":conformance_proto_upb", ":test_messages_proto3_proto_upb", @@ -513,7 +572,10 @@ cc_library( name = "amalgamation", srcs = ["upb.c"], hdrs = ["upb.h"], - copts = COPTS, + copts = select({ + ":windows": [], + "//conditions:default": COPTS + }), ) # Lua libraries. ############################################################### diff --git a/CMakeLists.txt b/CMakeLists.txt index c1c45df48b..836c5ff1fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ add_library(upb upb/generated_util.h upb/msg.c upb/msg.h + upb/port.c upb/port_def.inc upb/port_undef.inc upb/table.c diff --git a/bazel/build_defs.bzl b/bazel/build_defs.bzl index 1ec80b30b3..08bb44ee3f 100644 --- a/bazel/build_defs.bzl +++ b/bazel/build_defs.bzl @@ -1,6 +1,6 @@ """Internal rules for building upb.""" -load(":upb_proto_library.bzl", "GeneratedSrcs") +load(":upb_proto_library.bzl", "GeneratedSrcsInfo") def _librule(name): return name + "_lib" @@ -173,8 +173,8 @@ SrcList = provider( ) def _file_list_aspect_impl(target, ctx): - if GeneratedSrcs in target: - srcs = target[GeneratedSrcs] + if GeneratedSrcsInfo in target: + srcs = target[GeneratedSrcsInfo] return [SrcList(srcs = srcs.srcs + srcs.hdrs)] srcs = [] diff --git a/bazel/upb_proto_library.bzl b/bazel/upb_proto_library.bzl index bf2e33ad6f..f148745be5 100644 --- a/bazel/upb_proto_library.bzl +++ b/bazel/upb_proto_library.bzl @@ -22,6 +22,11 @@ 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. + if short_path.startswith("_virtual_imports"): + short_path = short_path.split("/", 2)[-1] return short_path def _get_real_root(file): @@ -134,7 +139,7 @@ def _cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos): # upb_proto_library / upb_proto_reflection_library shared code ################# -GeneratedSrcs = provider( +GeneratedSrcsInfo = provider( fields = { "srcs": "list of srcs", "hdrs": "list of hdrs", @@ -142,7 +147,7 @@ GeneratedSrcs = provider( ) _WrappedCcInfo = provider(fields = ["cc_info"]) -_WrappedGeneratedSrcs = provider(fields = ["srcs"]) +_WrappedGeneratedSrcsInfo = provider(fields = ["srcs"]) def _compile_upb_protos(ctx, proto_info, proto_sources, ext): srcs = [_generate_output_file(ctx, name, ext + ".c") for name in proto_sources] @@ -159,23 +164,26 @@ def _compile_upb_protos(ctx, proto_info, proto_sources, ext): arguments = [ "--upb_out=" + _get_real_root(srcs[0]), "--plugin=protoc-gen-upb=" + ctx.executable._upbc.path, - "--descriptor_set_in=" + ":".join([f.path for f in transitive_sets]), + "--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], progress_message = "Generating upb protos for :" + ctx.label.name, ) - return GeneratedSrcs(srcs = srcs, hdrs = hdrs) + return GeneratedSrcsInfo(srcs = srcs, hdrs = hdrs) def _upb_proto_rule_impl(ctx): if len(ctx.attr.deps) != 1: fail("only one deps dependency allowed.") dep = ctx.attr.deps[0] - if _WrappedCcInfo not in dep or _WrappedGeneratedSrcs not in dep: + if _WrappedCcInfo not in dep or _WrappedGeneratedSrcsInfo not in dep: fail("proto_library rule must generate _WrappedCcInfo and " + - "_WrappedGeneratedSrcs (aspect should have handled this).") + "_WrappedGeneratedSrcsInfo (aspect should have handled this).") cc_info = dep[_WrappedCcInfo].cc_info - srcs = dep[_WrappedGeneratedSrcs].srcs - lib = cc_info.linking_context.libraries_to_link.to_list()[0] + srcs = dep[_WrappedGeneratedSrcsInfo].srcs + 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] files = _filter_none([ lib.static_library, lib.pic_static_library, @@ -200,7 +208,7 @@ def _upb_proto_aspect_impl(target, ctx): srcs = files.srcs, dep_ccinfos = dep_ccinfos, ) - return [_WrappedCcInfo(cc_info = cc_info), _WrappedGeneratedSrcs(srcs = files)] + return [_WrappedCcInfo(cc_info = cc_info), _WrappedGeneratedSrcsInfo(srcs = files)] def _maybe_add(d): if not _is_bazel: diff --git a/generated_for_cmake/upb/json/parser.c b/generated_for_cmake/upb/json/parser.c index a1bd0d2784..f72e945881 100644 --- a/generated_for_cmake/upb/json/parser.c +++ b/generated_for_cmake/upb/json/parser.c @@ -949,7 +949,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 = 1.0 / 0.0; /* C89 does not have an INFINITY macro. */ + double inf = UPB_INFINITY; errno = 0; @@ -1191,7 +1191,8 @@ static bool start_any_stringval(upb_json_parser *p) { static bool start_stringval(upb_json_parser *p) { if (is_top_level(p)) { - if (is_string_wrapper_object(p)) { + if (is_string_wrapper_object(p) || + is_number_wrapper_object(p)) { start_wrapper_object(p); } else if (is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) { start_fieldmask_object(p); @@ -1204,7 +1205,8 @@ static bool start_stringval(upb_json_parser *p) { } else { return false; } - } else if (does_string_wrapper_start(p)) { + } else if (does_string_wrapper_start(p) || + does_number_wrapper_start(p)) { if (!start_subobject(p)) { return false; } @@ -1410,7 +1412,8 @@ static bool end_stringval(upb_json_parser *p) { return false; } - if (does_string_wrapper_end(p)) { + if (does_string_wrapper_end(p) || + does_number_wrapper_end(p)) { end_wrapper_object(p); if (!is_top_level(p)) { end_subobject(p); @@ -2586,11 +2589,11 @@ static bool does_fieldmask_end(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2791 "upb/json/parser.rl" +#line 2794 "upb/json/parser.rl" -#line 2594 "upb/json/parser.c" +#line 2597 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2845,7 +2848,7 @@ static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 2794 "upb/json/parser.rl" +#line 2797 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2868,7 +2871,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2872 "upb/json/parser.c" +#line 2875 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2943,147 +2946,147 @@ _match: switch ( *_acts++ ) { case 1: -#line 2599 "upb/json/parser.rl" +#line 2602 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2601 "upb/json/parser.rl" +#line 2604 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2605 "upb/json/parser.rl" +#line 2608 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2606 "upb/json/parser.rl" +#line 2609 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2612 "upb/json/parser.rl" +#line 2615 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2613 "upb/json/parser.rl" +#line 2616 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2614 "upb/json/parser.rl" +#line 2617 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2620 "upb/json/parser.rl" +#line 2623 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2626 "upb/json/parser.rl" +#line 2629 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2631 "upb/json/parser.rl" +#line 2634 "upb/json/parser.rl" { start_year(parser, p); } break; case 11: -#line 2632 "upb/json/parser.rl" +#line 2635 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_year(parser, p)); } break; case 12: -#line 2636 "upb/json/parser.rl" +#line 2639 "upb/json/parser.rl" { start_month(parser, p); } break; case 13: -#line 2637 "upb/json/parser.rl" +#line 2640 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_month(parser, p)); } break; case 14: -#line 2641 "upb/json/parser.rl" +#line 2644 "upb/json/parser.rl" { start_day(parser, p); } break; case 15: -#line 2642 "upb/json/parser.rl" +#line 2645 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_day(parser, p)); } break; case 16: -#line 2646 "upb/json/parser.rl" +#line 2649 "upb/json/parser.rl" { start_hour(parser, p); } break; case 17: -#line 2647 "upb/json/parser.rl" +#line 2650 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hour(parser, p)); } break; case 18: -#line 2651 "upb/json/parser.rl" +#line 2654 "upb/json/parser.rl" { start_minute(parser, p); } break; case 19: -#line 2652 "upb/json/parser.rl" +#line 2655 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_minute(parser, p)); } break; case 20: -#line 2656 "upb/json/parser.rl" +#line 2659 "upb/json/parser.rl" { start_second(parser, p); } break; case 21: -#line 2657 "upb/json/parser.rl" +#line 2660 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_second(parser, p)); } break; case 22: -#line 2662 "upb/json/parser.rl" +#line 2665 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 23: -#line 2663 "upb/json/parser.rl" +#line 2666 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 24: -#line 2665 "upb/json/parser.rl" +#line 2668 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 2670 "upb/json/parser.rl" +#line 2673 "upb/json/parser.rl" { start_timestamp_base(parser); } break; case 26: -#line 2672 "upb/json/parser.rl" +#line 2675 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 27: -#line 2673 "upb/json/parser.rl" +#line 2676 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 28: -#line 2675 "upb/json/parser.rl" +#line 2678 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 29: -#line 2676 "upb/json/parser.rl" +#line 2679 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 30: -#line 2678 "upb/json/parser.rl" +#line 2681 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 31: -#line 2683 "upb/json/parser.rl" +#line 2686 "upb/json/parser.rl" { start_fieldmask_path_text(parser, p); } break; case 32: -#line 2684 "upb/json/parser.rl" +#line 2687 "upb/json/parser.rl" { end_fieldmask_path_text(parser, p); } break; case 33: -#line 2689 "upb/json/parser.rl" +#line 2692 "upb/json/parser.rl" { start_fieldmask_path(parser); } break; case 34: -#line 2690 "upb/json/parser.rl" +#line 2693 "upb/json/parser.rl" { end_fieldmask_path(parser); } break; case 35: -#line 2696 "upb/json/parser.rl" +#line 2699 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 36: -#line 2701 "upb/json/parser.rl" +#line 2704 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -3097,11 +3100,11 @@ _match: } break; case 37: -#line 2714 "upb/json/parser.rl" +#line 2717 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; case 38: -#line 2719 "upb/json/parser.rl" +#line 2722 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -3111,11 +3114,11 @@ _match: } break; case 39: -#line 2726 "upb/json/parser.rl" +#line 2729 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 40: -#line 2729 "upb/json/parser.rl" +#line 2732 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -3125,7 +3128,7 @@ _match: } break; case 41: -#line 2740 "upb/json/parser.rl" +#line 2743 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -3135,7 +3138,7 @@ _match: } break; case 42: -#line 2749 "upb/json/parser.rl" +#line 2752 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -3145,54 +3148,54 @@ _match: } break; case 43: -#line 2761 "upb/json/parser.rl" +#line 2764 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 44: -#line 2765 "upb/json/parser.rl" +#line 2768 "upb/json/parser.rl" { end_array(parser); } break; case 45: -#line 2770 "upb/json/parser.rl" +#line 2773 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 46: -#line 2771 "upb/json/parser.rl" +#line 2774 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 47: -#line 2773 "upb/json/parser.rl" +#line 2776 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 48: -#line 2774 "upb/json/parser.rl" +#line 2777 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 49: -#line 2776 "upb/json/parser.rl" +#line 2779 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 50: -#line 2778 "upb/json/parser.rl" +#line 2781 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 51: -#line 2780 "upb/json/parser.rl" +#line 2783 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 52: -#line 2782 "upb/json/parser.rl" +#line 2785 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 53: -#line 2783 "upb/json/parser.rl" +#line 2786 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 54: -#line 2788 "upb/json/parser.rl" +#line 2791 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 3196 "upb/json/parser.c" +#line 3199 "upb/json/parser.c" } } @@ -3209,32 +3212,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2597 "upb/json/parser.rl" +#line 2600 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 46: -#line 2771 "upb/json/parser.rl" +#line 2774 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 49: -#line 2776 "upb/json/parser.rl" +#line 2779 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 50: -#line 2778 "upb/json/parser.rl" +#line 2781 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 51: -#line 2780 "upb/json/parser.rl" +#line 2783 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 53: -#line 2783 "upb/json/parser.rl" +#line 2786 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 3238 "upb/json/parser.c" +#line 3241 "upb/json/parser.c" } } } @@ -3242,7 +3245,7 @@ goto _again;} } _out: {} } -#line 2816 "upb/json/parser.rl" +#line 2819 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3285,13 +3288,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3289 "upb/json/parser.c" +#line 3292 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2858 "upb/json/parser.rl" +#line 2861 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -3395,7 +3398,7 @@ const upb_byteshandler *upb_json_parsermethod_inputhandler( return &m->input_handler_; } -upb_json_codecache *upb_json_codecache_new() { +upb_json_codecache *upb_json_codecache_new(void) { upb_alloc *alloc; upb_json_codecache *c; diff --git a/tests/conformance_upb.c b/tests/conformance_upb.c index 36e550eefc..6063c9941e 100644 --- a/tests/conformance_upb.c +++ b/tests/conformance_upb.c @@ -119,7 +119,7 @@ void DoTest( return; } -bool DoTestIo() { +bool DoTestIo(void) { upb_arena *arena; upb_alloc *alloc; upb_status status; @@ -168,7 +168,7 @@ bool DoTestIo() { return true; } -int main() { +int main(void) { while (1) { if (!DoTestIo()) { fprintf(stderr, "conformance_upb: received EOF from test runner " diff --git a/tests/corpus/README b/tests/corpus/README new file mode 100644 index 0000000000..9bd8f1e93d --- /dev/null +++ b/tests/corpus/README @@ -0,0 +1 @@ +Corpus folder for fuzzing diff --git a/tests/corpus/temp.cc b/tests/corpus/temp.cc new file mode 100644 index 0000000000..2bf1160ce6 --- /dev/null +++ b/tests/corpus/temp.cc @@ -0,0 +1 @@ +// Hello World diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index fb69541023..5cc59e40c9 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -447,12 +447,12 @@ upb::pb::DecoderPtr CreateDecoder(upb::Arena* arena, uint32_t Hash(const string& proto, const string* expected_output, size_t seam1, size_t seam2, bool may_skip) { - uint32_t hash = MurmurHash2(proto.c_str(), proto.size(), 0); + uint32_t hash = upb_murmur_hash2(proto.c_str(), proto.size(), 0); if (expected_output) - hash = MurmurHash2(expected_output->c_str(), expected_output->size(), hash); - hash = MurmurHash2(&seam1, sizeof(seam1), hash); - hash = MurmurHash2(&seam2, sizeof(seam2), hash); - hash = MurmurHash2(&may_skip, sizeof(may_skip), hash); + 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; } diff --git a/tests/pb/test_varint.c b/tests/pb/test_varint.c index ebee282571..95a04ab9b8 100644 --- a/tests/pb/test_varint.c +++ b/tests/pb/test_varint.c @@ -101,7 +101,7 @@ static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) { upb_decoderet _upb_vdecode_ ## decoder(const char *p) { \ return upb_vdecode_ ## decoder(p); \ } \ - void test_ ## decoder() { \ + void test_ ## decoder(void) { \ test_varint_decoder(&_upb_vdecode_ ## decoder); \ } \ diff --git a/upb/decode.c b/upb/decode.c index 5ba1f05d77..4f07988595 100644 --- a/upb/decode.c +++ b/upb/decode.c @@ -74,7 +74,7 @@ static bool upb_decode_varint32(const char **ptr, const char *limit, uint32_t *val) { uint64_t u64; CHK(upb_decode_varint(ptr, limit, &u64) && u64 <= UINT32_MAX); - *val = u64; + *val = (uint32_t)u64; return true; } @@ -336,7 +336,7 @@ static bool upb_decode_varintfield(upb_decstate *d, upb_decframe *frame, case UPB_DESCRIPTOR_TYPE_INT32: case UPB_DESCRIPTOR_TYPE_UINT32: case UPB_DESCRIPTOR_TYPE_ENUM: { - uint32_t val32 = val; + uint32_t val32 = (uint32_t)val; CHK(upb_decode_addval(frame, field, &val32, sizeof(val32))); break; } @@ -346,7 +346,7 @@ static bool upb_decode_varintfield(upb_decstate *d, upb_decframe *frame, break; } case UPB_DESCRIPTOR_TYPE_SINT32: { - int32_t decoded = upb_zzdecode_32(val); + int32_t decoded = upb_zzdecode_32((uint32_t)val); CHK(upb_decode_addval(frame, field, &decoded, sizeof(decoded))); break; } @@ -425,7 +425,10 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, upb_array *arr = upb_getorcreatearr(frame, field); CHK(arr); -#define VARINT_CASE(ctype, decode) \ +#define VARINT_CASE(ctype, decode) \ + VARINT_CASE_EX(ctype, decode, decode) + +#define VARINT_CASE_EX(ctype, decode, dtype) \ { \ const char *ptr = d->ptr; \ const char *limit = ptr + len; \ @@ -433,7 +436,7 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, uint64_t val; \ ctype decoded; \ CHK(upb_decode_varint(&ptr, limit, &val)); \ - decoded = (decode)(val); \ + decoded = (decode)((dtype)val); \ CHK(upb_array_add(arr, 1, sizeof(decoded), &decoded, d->arena)); \ } \ d->ptr = ptr; \ @@ -464,9 +467,9 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, case UPB_DESCRIPTOR_TYPE_BOOL: VARINT_CASE(bool, bool); case UPB_DESCRIPTOR_TYPE_SINT32: - VARINT_CASE(int32_t, upb_zzdecode_32); + VARINT_CASE_EX(int32_t, upb_zzdecode_32, uint32_t); case UPB_DESCRIPTOR_TYPE_SINT64: - VARINT_CASE(int64_t, upb_zzdecode_64); + VARINT_CASE_EX(int64_t, upb_zzdecode_64, uint64_t); case UPB_DESCRIPTOR_TYPE_MESSAGE: { const upb_msglayout *subm; upb_msg *submsg = upb_addmsg(frame, field, &subm); @@ -570,6 +573,7 @@ static bool upb_decode_field(upb_decstate *d, upb_decframe *frame) { CHK(upb_append_unknown(d, frame)); return true; } + UPB_UNREACHABLE(); } static bool upb_decode_message(upb_decstate *d, char *msg, const upb_msglayout *l) { diff --git a/upb/def.c b/upb/def.c index b51f22a546..98aead6b67 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1029,6 +1029,7 @@ static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, } else { return false; } + break; } case UPB_TYPE_STRING: f->defaultval.str = newstr(ctx->alloc, str, len); @@ -1633,7 +1634,7 @@ void upb_symtab_free(upb_symtab *s) { upb_gfree(s); } -upb_symtab *upb_symtab_new() { +upb_symtab *upb_symtab_new(void) { upb_symtab *s = upb_gmalloc(sizeof(*s)); upb_alloc *alloc; diff --git a/upb/def.h b/upb/def.h index c88ce90f93..9be285794e 100644 --- a/upb/def.h +++ b/upb/def.h @@ -837,7 +837,7 @@ class upb::FileDefPtr { extern "C" { #endif -upb_symtab *upb_symtab_new(); +upb_symtab *upb_symtab_new(void); void upb_symtab_free(upb_symtab* s); const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); const upb_msgdef *upb_symtab_lookupmsg2( diff --git a/upb/encode.c b/upb/encode.c index 0f1580374a..43d24cdbfc 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -25,8 +25,8 @@ static size_t upb_encode_varint(uint64_t val, char *buf) { return i; } -static uint32_t upb_zzencode_32(int32_t n) { return (n << 1) ^ (n >> 31); } -static uint64_t upb_zzencode_64(int64_t n) { return (n << 1) ^ (n >> 63); } +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); } typedef struct { upb_alloc *alloc; @@ -48,7 +48,9 @@ static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) { CHK(new_buf); /* We want previous data at the end, realloc() put it at the beginning. */ - memmove(new_buf + new_size - old_size, e->buf, old_size); + if (old_size > 0) { + memmove(new_buf + new_size - old_size, e->buf, old_size); + } e->ptr = new_buf + new_size - (e->limit - e->ptr); e->limit = new_buf + new_size; diff --git a/upb/generated_util.h b/upb/generated_util.h index a0b3e8df4c..234bcdad3c 100644 --- a/upb/generated_util.h +++ b/upb/generated_util.h @@ -87,11 +87,11 @@ UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) { } UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) { - return (*PTR_AT(msg, idx / 8, char)) |= (1 << (idx % 8)); + return (*PTR_AT(msg, idx / 8, char)) |= (char)(1 << (idx % 8)); } UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) { - return (*PTR_AT(msg, idx / 8, char)) &= ~(1 << (idx % 8)); + return (*PTR_AT(msg, idx / 8, char)) &= (char)(~(1 << (idx % 8))); } UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) { diff --git a/upb/json/parser.h b/upb/json/parser.h index c063a77ef7..d323c52ef8 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -113,7 +113,7 @@ typedef struct upb_json_codecache upb_json_codecache; extern "C" { #endif -upb_json_codecache *upb_json_codecache_new(); +upb_json_codecache *upb_json_codecache_new(void); void upb_json_codecache_free(upb_json_codecache *cache); const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, const upb_msgdef* md); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index fd3704cbdf..2641dda31c 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -947,7 +947,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 = 1.0 / 0.0; /* C89 does not have an INFINITY macro. */ + double inf = UPB_INFINITY; errno = 0; @@ -1189,7 +1189,8 @@ static bool start_any_stringval(upb_json_parser *p) { static bool start_stringval(upb_json_parser *p) { if (is_top_level(p)) { - if (is_string_wrapper_object(p)) { + if (is_string_wrapper_object(p) || + is_number_wrapper_object(p)) { start_wrapper_object(p); } else if (is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) { start_fieldmask_object(p); @@ -1202,7 +1203,8 @@ static bool start_stringval(upb_json_parser *p) { } else { return false; } - } else if (does_string_wrapper_start(p)) { + } else if (does_string_wrapper_start(p) || + does_number_wrapper_start(p)) { if (!start_subobject(p)) { return false; } @@ -1408,7 +1410,8 @@ static bool end_stringval(upb_json_parser *p) { return false; } - if (does_string_wrapper_end(p)) { + if (does_string_wrapper_end(p) || + does_number_wrapper_end(p)) { end_wrapper_object(p); if (!is_top_level(p)) { end_subobject(p); @@ -2958,7 +2961,7 @@ const upb_byteshandler *upb_json_parsermethod_inputhandler( return &m->input_handler_; } -upb_json_codecache *upb_json_codecache_new() { +upb_json_codecache *upb_json_codecache_new(void) { upb_alloc *alloc; upb_json_codecache *c; diff --git a/upb/json/printer.c b/upb/json/printer.c index 37482a8a3b..38f817d4d4 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -181,11 +181,11 @@ const char neginf[] = "\"-Infinity\""; const char inf[] = "\"Infinity\""; static size_t fmt_double(double val, char* buf, size_t length) { - if (val == (1.0 / 0.0)) { + if (val == UPB_INFINITY) { CHKLENGTH(length >= strlen(inf)); strcpy(buf, inf); return strlen(inf); - } else if (val == (-1.0 / 0.0)) { + } else if (val == -UPB_INFINITY) { CHKLENGTH(length >= strlen(neginf)); strcpy(buf, neginf); return strlen(neginf); @@ -208,18 +208,32 @@ static size_t fmt_bool(bool val, char* buf, size_t length) { return n; } -static size_t fmt_int64(long val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%ld", val); +static size_t fmt_int64_as_number(long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "%lld", val); CHKLENGTH(n > 0 && n < length); return n; } -static size_t fmt_uint64(unsigned long long val, char* buf, size_t length) { +static size_t fmt_uint64_as_number( + unsigned long long val, char* buf, size_t length) { size_t n = _upb_snprintf(buf, length, "%llu", val); CHKLENGTH(n > 0 && n < length); return n; } +static size_t fmt_int64_as_string(long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "\"%lld\"", val); + CHKLENGTH(n > 0 && n < length); + return n; +} + +static size_t fmt_uint64_as_string( + unsigned long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "\"%llu\"", val); + CHKLENGTH(n > 0 && n < length); + return n; +} + /* Print a map key given a field name. Called by scalar field handlers and by * startseq for repeated fields. */ static bool putkey(void *closure, const void *handler_data) { @@ -263,8 +277,11 @@ static bool putkey(void *closure, const void *handler_data) { static bool putmapkey_##type(void *closure, const void *handler_data, \ type val) { \ upb_json_printer *p = closure; \ + char data[64]; \ + size_t length = fmt_func(val, data, sizeof(data)); \ + UPB_UNUSED(handler_data); \ print_data(p, "\"", 1); \ - CHK(put##type(closure, handler_data, val)); \ + print_data(p, data, length); \ print_data(p, "\":", 2); \ return true; \ } @@ -272,17 +289,17 @@ static bool putkey(void *closure, const void *handler_data) { TYPE_HANDLERS(double, fmt_double) TYPE_HANDLERS(float, fmt_float) TYPE_HANDLERS(bool, fmt_bool) -TYPE_HANDLERS(int32_t, fmt_int64) -TYPE_HANDLERS(uint32_t, fmt_int64) -TYPE_HANDLERS(int64_t, fmt_int64) -TYPE_HANDLERS(uint64_t, fmt_uint64) +TYPE_HANDLERS(int32_t, fmt_int64_as_number) +TYPE_HANDLERS(uint32_t, fmt_int64_as_number) +TYPE_HANDLERS(int64_t, fmt_int64_as_string) +TYPE_HANDLERS(uint64_t, fmt_uint64_as_string) /* double and float are not allowed to be map keys. */ TYPE_HANDLERS_MAPKEY(bool, fmt_bool) -TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64) +TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64_as_number) #undef TYPE_HANDLERS #undef TYPE_HANDLERS_MAPKEY diff --git a/upb/legacy_msg_reflection.c b/upb/legacy_msg_reflection.c index aa043d1cb7..9ab4b6fd5d 100644 --- a/upb/legacy_msg_reflection.c +++ b/upb/legacy_msg_reflection.c @@ -292,7 +292,7 @@ struct upb_mapiter { upb_fieldtype_t key_type; }; -size_t upb_mapiter_sizeof() { +size_t upb_mapiter_sizeof(void) { return sizeof(upb_mapiter); } diff --git a/upb/legacy_msg_reflection.h b/upb/legacy_msg_reflection.h index b2dbb2f0da..dc23913224 100644 --- a/upb/legacy_msg_reflection.h +++ b/upb/legacy_msg_reflection.h @@ -171,7 +171,7 @@ bool upb_map_del(upb_map *map, upb_fieldtype_t key_type, upb_msgval key, * and if you keep invalidating the iterator during iteration, the program may * enter an infinite loop. */ -size_t upb_mapiter_sizeof(); +size_t upb_mapiter_sizeof(void); void upb_mapiter_begin(upb_mapiter *i, upb_fieldtype_t key_type, const upb_map *t); diff --git a/upb/msg.c b/upb/msg.c index 74bb62139f..48d6788f7b 100644 --- a/upb/msg.c +++ b/upb/msg.c @@ -5,7 +5,7 @@ #include "upb/port_def.inc" -#define VOIDPTR_AT(msg, ofs) (void*)((char*)msg + ofs) +#define VOIDPTR_AT(msg, ofs) (void*)((char*)msg + (int)ofs) /* Internal members of a upb_msg. We can change this without breaking binary * compatibility. We put these before the user's data. The user's upb_msg* diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c index a7d1ac3fbb..f5c7d65646 100644 --- a/upb/pb/compile_decoder.c +++ b/upb/pb/compile_decoder.c @@ -69,7 +69,7 @@ static void freegroup(mgroup *g) { upb_gfree(g); } -mgroup *newgroup() { +mgroup *newgroup(void) { mgroup *g = upb_gmalloc(sizeof(*g)); upb_inttable_init(&g->methods, UPB_CTYPE_PTR); g->bytecode = NULL; @@ -149,7 +149,7 @@ static int32_t getofs(uint32_t instruction) { static void setofs(uint32_t *instruction, int32_t ofs) { if (op_has_longofs(*instruction)) { - *instruction = getop(*instruction) | ofs << 8; + *instruction = getop(*instruction) | (uint32_t)ofs << 8; } else { *instruction = (*instruction & ~0xff00) | ((ofs & 0xff) << 8); } @@ -822,12 +822,10 @@ static void set_bytecode_handlers(mgroup *g) { /* TODO(haberman): allow this to be constructed for an arbitrary set of dest * handlers and other mgroups (but verify we have a transitive closure). */ -const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy) { +const mgroup *mgroup_new(const upb_handlers *dest, bool lazy) { mgroup *g; compiler *c; - UPB_UNUSED(allowjit); - g = newgroup(); c = newcompiler(g, lazy); find_methods(c, dest); @@ -872,7 +870,6 @@ upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) { if (!c) return NULL; c->dest = dest; - c->allow_jit = true; c->lazy = false; c->arena = upb_arena_new(); @@ -882,13 +879,12 @@ upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) { } void upb_pbcodecache_free(upb_pbcodecache *c) { - size_t i; + upb_inttable_iter i; - for (i = 0; i < upb_inttable_count(&c->groups); i++) { - upb_value v; - bool ok = upb_inttable_lookup(&c->groups, i, &v); - UPB_ASSERT(ok); - freegroup((void*)upb_value_getconstptr(v)); + upb_inttable_begin(&i, &c->groups); + for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + upb_value val = upb_inttable_iter_value(&i); + freegroup((void*)upb_value_getconstptr(val)); } upb_inttable_uninit(&c->groups); @@ -896,15 +892,6 @@ void upb_pbcodecache_free(upb_pbcodecache *c) { upb_gfree(c); } -bool upb_pbcodecache_allowjit(const upb_pbcodecache *c) { - return c->allow_jit; -} - -void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow) { - UPB_ASSERT(upb_inttable_count(&c->groups) == 0); - c->allow_jit = allow; -} - void upb_pbdecodermethodopts_setlazy(upb_pbcodecache *c, bool lazy) { UPB_ASSERT(upb_inttable_count(&c->groups) == 0); c->lazy = lazy; @@ -917,11 +904,14 @@ const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c, const upb_handlers *h; const mgroup *g; - /* Right now we build a new DecoderMethod every time. - * TODO(haberman): properly cache methods by their true key. */ h = upb_handlercache_get(c->dest, md); - g = mgroup_new(h, c->allow_jit, c->lazy); - upb_inttable_push(&c->groups, upb_value_constptr(g)); + if (upb_inttable_lookupptr(&c->groups, md, &v)) { + g = upb_value_getconstptr(v); + } else { + g = mgroup_new(h, c->lazy); + ok = upb_inttable_insertptr(&c->groups, md, upb_value_constptr(g)); + UPB_ASSERT(ok); + } ok = upb_inttable_lookupptr(&g->methods, h, &v); UPB_ASSERT(ok); diff --git a/upb/pb/decoder.c b/upb/pb/decoder.c index a7a5e7b6c7..735bef18f8 100644 --- a/upb/pb/decoder.c +++ b/upb/pb/decoder.c @@ -77,16 +77,6 @@ static size_t stacksize(upb_pbdecoder *d, size_t entries) { static size_t callstacksize(upb_pbdecoder *d, size_t entries) { UPB_UNUSED(d); -#ifdef UPB_USE_JIT_X64 - if (d->method_->is_native_) { - /* Each native stack frame needs two pointers, plus we need a few frames for - * the enter/exit trampolines. */ - size_t ret = entries * sizeof(void*) * 2; - ret += sizeof(void*) * 10; - return ret; - } -#endif - return entries * sizeof(uint32_t*); } @@ -902,17 +892,6 @@ void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint) { return d; } -void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint) { - upb_pbdecoder *d = closure; - UPB_UNUSED(hd); - UPB_UNUSED(size_hint); - d->top->end_ofs = UINT64_MAX; - d->bufstart_ofs = 0; - d->call_len = 0; - d->skip = 0; - return d; -} - bool upb_pbdecoder_end(void *closure, const void *handler_data) { upb_pbdecoder *d = closure; const upb_pbdecodermethod *method = handler_data; @@ -938,14 +917,6 @@ bool upb_pbdecoder_end(void *closure, const void *handler_data) { end = offset(d); d->top->end_ofs = end; -#ifdef UPB_USE_JIT_X64 - if (method->is_native_) { - const mgroup *group = (const mgroup*)method->group; - if (d->top != d->stack) - d->stack->end_ofs = 0; - group->jit_code(closure, method->code_base.ptr, &dummy, 0, NULL); - } else -#endif { const uint32_t *p = d->pc; d->stack->end_ofs = end; diff --git a/upb/pb/decoder.int.h b/upb/pb/decoder.int.h index 3ed50dfcbc..9d5f5839bc 100644 --- a/upb/pb/decoder.int.h +++ b/upb/pb/decoder.int.h @@ -80,7 +80,7 @@ struct upb_pbcodecache { bool allow_jit; bool lazy; - /* Array of mgroups. */ + /* Map of upb_msgdef -> mgroup. */ upb_inttable groups; }; @@ -96,15 +96,6 @@ typedef struct { /* The bytecode for our methods, if any exists. Owned by us. */ uint32_t *bytecode; uint32_t *bytecode_end; - -#ifdef UPB_USE_JIT_X64 - /* JIT-generated machine code, if any. */ - upb_string_handlerfunc *jit_code; - /* The size of the jit_code (required to munmap()). */ - size_t jit_size; - char *debug_info; - void *dl; -#endif } mgroup; /* The maximum that any submessages can be nested. Matches proto2's limit. @@ -215,19 +206,10 @@ struct upb_pbdecoder { size_t stack_size; upb_status *status; - -#ifdef UPB_USE_JIT_X64 - /* Used momentarily by the generated code to store a value while a user - * function is called. */ - uint32_t tmp_len; - - const void *saved_rsp; -#endif }; /* Decoder entry points; used as handlers. */ void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint); -void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint); size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle); bool upb_pbdecoder_end(void *closure, const void *handler_data); @@ -251,10 +233,6 @@ extern const char *kPbDecoderSubmessageTooLong; /* Access to decoderplan members needed by the decoder. */ const char *upb_pbdecoder_getopname(unsigned int op); -/* JIT codegen entry point. */ -void upb_pbdecoder_jit(mgroup *group); -void upb_pbdecoder_freejit(mgroup *group); - /* A special label that means "do field dispatch for this message and branch to * wherever that takes you." */ #define LABEL_DISPATCH 0 diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c index e7092a8650..d108a643d3 100644 --- a/upb/pb/encoder.c +++ b/upb/pb/encoder.c @@ -524,7 +524,7 @@ void upb_pb_encoder_reset(upb_pb_encoder *e) { /* public API *****************************************************************/ -upb_handlercache *upb_pb_encoder_newcache() { +upb_handlercache *upb_pb_encoder_newcache(void) { return upb_handlercache_new(newhandlers_callback, NULL); } diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h index 1113c3ac2d..f125b37218 100644 --- a/upb/pb/encoder.h +++ b/upb/pb/encoder.h @@ -45,7 +45,7 @@ upb_pb_encoder* upb_pb_encoder_create(upb_arena* a, const upb_handlers* h, /* Lazily builds and caches handlers that will push encoded data to a bytessink. * Any msgdef objects used with this object must outlive it. */ -upb_handlercache *upb_pb_encoder_newcache(); +upb_handlercache *upb_pb_encoder_newcache(void); #ifdef __cplusplus } /* extern "C" { */ diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c index 7aafffc998..0760173e43 100644 --- a/upb/pb/textprinter.c +++ b/upb/pb/textprinter.c @@ -329,7 +329,7 @@ upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h, return p; } -upb_handlercache *upb_textprinter_newcache() { +upb_handlercache *upb_textprinter_newcache(void) { return upb_handlercache_new(&onmreg, NULL); } diff --git a/upb/pb/textprinter.h b/upb/pb/textprinter.h index 0af2b1a4d5..7e20d7521b 100644 --- a/upb/pb/textprinter.h +++ b/upb/pb/textprinter.h @@ -31,7 +31,7 @@ upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h, upb_bytessink output); void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line); upb_sink upb_textprinter_input(upb_textprinter *p); -upb_handlercache *upb_textprinter_newcache(); +upb_handlercache *upb_textprinter_newcache(void); #ifdef __cplusplus } /* extern "C" */ diff --git a/upb/pb/varint.int.h b/upb/pb/varint.int.h index ddda694312..ff1ca66149 100644 --- a/upb/pb/varint.int.h +++ b/upb/pb/varint.int.h @@ -46,8 +46,12 @@ UPB_INLINE int32_t upb_zzdec_32(uint32_t n) { UPB_INLINE int64_t upb_zzdec_64(uint64_t n) { return (n >> 1) ^ -(int64_t)(n & 1); } -UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); } -UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); } +UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { + return ((uint32_t)n << 1) ^ (n >> 31); +} +UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { + return ((uint64_t)n << 1) ^ (n >> 63); +} /* Decoding *******************************************************************/ diff --git a/upb/port.c b/upb/port.c new file mode 100644 index 0000000000..023f7dc0ce --- /dev/null +++ b/upb/port.c @@ -0,0 +1,27 @@ + +#include "upb/upb.h" +#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 diff --git a/upb/port_def.inc b/upb/port_def.inc index 1b343d870c..a8967b3679 100644 --- a/upb/port_def.inc +++ b/upb/port_def.inc @@ -45,14 +45,20 @@ /* UPB_INLINE: inline if possible, emit standalone code if required. */ #ifdef __cplusplus #define UPB_INLINE inline -#elif defined (__GNUC__) +#elif defined (__GNUC__) || defined(__clang__) #define UPB_INLINE static __inline__ #else #define UPB_INLINE static #endif /* Hints to the compiler about likely/unlikely branches. */ +#if defined (__GNUC__) || defined(__clang__) #define UPB_LIKELY(x) __builtin_expect((x),1) +#define UPB_UNLIKELY(x) __builtin_expect((x),0) +#else +#define UPB_LIKELY(x) (x) +#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. */ @@ -77,6 +83,21 @@ #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 +#include +#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! @@ -93,7 +114,7 @@ #ifdef __cplusplus #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \ (defined(_MSC_VER) && _MSC_VER >= 1900) -// C++11 is present +/* C++11 is present */ #else #error upb requires C++11 for C++ support #endif @@ -116,8 +137,16 @@ * exist in debug mode. This turns into regular assert. */ #define UPB_ASSERT_DEBUGVAR(expr) assert(expr) -#ifdef __GNUC__ +#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 +#ifdef INFINITY +#define UPB_INFINITY INFINITY +#else +#define UPB_INFINITY (1.0 / 0.0) +#endif diff --git a/upb/port_undef.inc b/upb/port_undef.inc index bfd2a084a4..103180b7fb 100644 --- a/upb/port_undef.inc +++ b/upb/port_undef.inc @@ -14,6 +14,8 @@ #undef UPB_ASSERT #undef UPB_ASSERT_DEBUGVAR #undef UPB_UNREACHABLE +#undef UPB_INFINITY +#undef UPB_MSVC_VSNPRINTF #undef _upb_snprintf #undef _upb_vsnprintf #undef _upb_va_copy diff --git a/upb/table.c b/upb/table.c index 429861e157..8896d217db 100644 --- a/upb/table.c +++ b/upb/table.c @@ -276,14 +276,15 @@ static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) { char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1); if (str == NULL) return 0; memcpy(str, &len, sizeof(uint32_t)); - memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len + 1); + memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len); + str[sizeof(uint32_t) + k2.str.len] = '\0'; return (uintptr_t)str; } static uint32_t strhash(upb_tabkey key) { uint32_t len; char *str = upb_tabstr(key, &len); - return MurmurHash2(str, len, 0); + return upb_murmur_hash2(str, len, 0); } static bool streql(upb_tabkey k1, lookupkey_t k2) { @@ -344,20 +345,20 @@ bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len, tabkey = strcopy(key, a); if (tabkey == 0) return false; - hash = MurmurHash2(key.str.str, key.str.len, 0); + hash = upb_murmur_hash2(key.str.str, key.str.len, 0); 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 = MurmurHash2(key, len, 0); + uint32_t hash = upb_murmur_hash2(key, len, 0); 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 = MurmurHash2(key, len, 0); + uint32_t hash = upb_murmur_hash2(key, len, 0); upb_tabkey tabkey; if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) { upb_free(alloc, (void*)tabkey); @@ -646,7 +647,7 @@ void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) { size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */ size_t hash_count = upb_inttable_count(t) - arr_count; size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0; - size_t hashsize_lg2 = log2ceil(hash_size); + int hashsize_lg2 = log2ceil(hash_size); upb_inttable_sizedinit(&new_t, t->t.ctype, arr_size, hashsize_lg2, a); upb_inttable_begin(&i, t); @@ -743,7 +744,7 @@ bool upb_inttable_iter_isequal(const upb_inttable_iter *i1, * 1. It will not work incrementally. * 2. It will not produce the same results on little-endian and big-endian * machines. */ -uint32_t MurmurHash2(const void *key, size_t len, uint32_t seed) { +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; @@ -794,11 +795,11 @@ uint32_t MurmurHash2(const void *key, size_t len, uint32_t seed) { #define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; } -uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed) { +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 = seed ^ len; + uint32_t h = (uint32_t)(seed ^ len); uint8_t align = (uintptr_t)data & 3; if(align && (len >= 4)) { diff --git a/upb/table.int.h b/upb/table.int.h index 762d449e80..23b0b2f221 100644 --- a/upb/table.int.h +++ b/upb/table.int.h @@ -245,7 +245,7 @@ UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) { } /* Used by some of the unit tests for generic hashing functionality. */ -uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed); +uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed); UPB_INLINE uintptr_t upb_intkey(uintptr_t key) { return key; diff --git a/upb/upb.h b/upb/upb.h index 7b5d1c1845..79c19d281e 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -159,8 +159,16 @@ UPB_INLINE void upb_free(upb_alloc *alloc, void *ptr) { /* The global allocator used by upb. Uses the standard malloc()/free(). */ +#ifdef __cplusplus +extern "C" { +#endif + extern upb_alloc upb_alloc_global; +#ifdef __cplusplus +} /* extern "C" */ +#endif + /* Functions that hard-code the global malloc. * * We still get benefit because we can put custom logic into our global @@ -222,7 +230,7 @@ UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize, return upb_realloc(upb_arena_alloc(a), ptr, oldsize, size); } -UPB_INLINE upb_arena *upb_arena_new() { +UPB_INLINE upb_arena *upb_arena_new(void) { return upb_arena_init(NULL, 0, &upb_alloc_global); } @@ -287,7 +295,7 @@ template class upb::InlinedArena : public upb::Arena { /* Constants ******************************************************************/ /* Generic function type. */ -typedef void upb_func(); +typedef void upb_func(void); /* A list of types as they are encoded on-the-wire. */ typedef enum {