Merge branch 'master' into maps

pull/13171/head
Joshua Haberman 5 years ago
commit 0a07f2714b
  1. 37
      BUILD
  2. 6
      bazel/build_defs.bzl
  3. 20
      bazel/upb_proto_library.bzl
  4. 1
      tests/corpus/README
  5. 1
      tests/corpus/temp.cc
  6. 15
      tests/file_descriptor_parsenew_fuzzer.cc
  7. 6
      tools/make_cmakelists.py
  8. 41
      upb/json/printer.c

37
BUILD

@ -9,7 +9,6 @@ load(
"make_shell_script",
"upb_amalgamation",
)
load(
"//bazel:upb_proto_library.bzl",
"upb_proto_library",
@ -43,6 +42,11 @@ config_setting(
visibility = ["//visibility:public"],
)
config_setting(
name = "fuzz",
values = {"define": "fuzz=true"},
)
# Public C/C++ libraries #######################################################
cc_library(
@ -76,23 +80,23 @@ 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,
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(
@ -356,6 +360,25 @@ cc_test(
],
)
# OSS-Fuzz test
cc_binary(
name = "file_descriptor_parsenew_fuzzer",
testonly = 1,
srcs = ["tests/file_descriptor_parsenew_fuzzer.cc"],
copts = CPPOPTS + select({
"//conditions:default": [],
":fuzz": ["-fsanitize=fuzzer,address"],
}),
defines = select({
"//conditions:default": [],
":fuzz": ["HAVE_FUZZER"],
}),
deps = [
":descriptor_upbproto",
":upb",
],
)
# copybara:strip_for_google3_begin
upb_proto_reflection_library(
name = "descriptor_upbreflection",

@ -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 = []

@ -37,10 +37,8 @@ def _get_real_roots(files):
return roots.keys()
def _generate_output_file(ctx, src, extension):
if _is_bazel:
real_short_path = _get_real_short_path(src)
else:
real_short_path = paths.relativize(src.short_path, ctx.label.package)
real_short_path = paths.relativize(real_short_path, ctx.label.package)
output_filename = paths.replace_extension(real_short_path, extension)
ret = ctx.actions.declare_file(output_filename)
return ret
@ -136,7 +134,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",
@ -144,7 +142,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]
@ -166,18 +164,18 @@ def _compile_upb_protos(ctx, proto_info, proto_sources, ext):
[_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[0]
srcs = dep[_WrappedGeneratedSrcsInfo].srcs
lib = cc_info.linking_context.libraries_to_link.to_list()[0]
files = _filter_none([
lib.static_library,
lib.pic_static_library,
@ -202,7 +200,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:

@ -0,0 +1 @@
Corpus folder for fuzzing

@ -0,0 +1 @@
// Hello World

@ -0,0 +1,15 @@
#include <cstdint>
#include "google/protobuf/descriptor.upb.h"
#include "upb/upb.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
upb::Arena arena;
google_protobuf_FileDescriptorProto_parse(reinterpret_cast<const char*>(data),
size, arena.ptr());
return 0;
}
#ifndef HAVE_FUZZER
int main() {}
#endif

@ -48,7 +48,7 @@ class BuildFileFunctions(object):
else:
print("Warning: no such file: " + file)
if filter(IsSourceFile, files):
if list(filter(IsSourceFile, files)):
# Has sources, make this a normal library.
self.converter.toplevel += "add_library(%s\n %s)\n" % (
kwargs["name"],
@ -272,8 +272,8 @@ def GetDict(obj):
globs = GetDict(converter)
execfile("WORKSPACE", GetDict(WorkspaceFileFunctions(converter)))
execfile("BUILD", GetDict(BuildFileFunctions(converter)))
exec(open("WORKSPACE").read(), GetDict(WorkspaceFileFunctions(converter)))
exec(open("BUILD").read(), GetDict(BuildFileFunctions(converter)))
with open(sys.argv[1], "w") as f:
f.write(converter.convert())

@ -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

Loading…
Cancel
Save