From dd45c0b9fd9958e18695f33986a883884ec56b7f Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 29 Aug 2016 17:05:43 -0700 Subject: Merge pull request #2012 from haberman/rubymapgcfix Ruby: make sure map parsing frames are GC-rooted. --- ruby/ext/google/protobuf_c/encode_decode.c | 62 ++++++++++++++++++++++++++---- ruby/ext/google/protobuf_c/protobuf.c | 2 + ruby/ext/google/protobuf_c/protobuf.h | 2 + ruby/ext/google/protobuf_c/upb.c | 2 +- 4 files changed, 59 insertions(+), 9 deletions(-) (limited to 'ruby') diff --git a/ruby/ext/google/protobuf_c/encode_decode.c b/ruby/ext/google/protobuf_c/encode_decode.c index dd21a046..08c72bcc 100644 --- a/ruby/ext/google/protobuf_c/encode_decode.c +++ b/ruby/ext/google/protobuf_c/encode_decode.c @@ -255,10 +255,54 @@ typedef struct { // value into the map. typedef struct { VALUE map; + const map_handlerdata_t* handlerdata; char key_storage[NATIVE_SLOT_MAX_SIZE]; char value_storage[NATIVE_SLOT_MAX_SIZE]; } map_parse_frame_t; +static void MapParseFrame_mark(void* _self) { + map_parse_frame_t* frame = _self; + + // This shouldn't strictly be necessary since this should be rooted by the + // message itself, but it can't hurt. + rb_gc_mark(frame->map); + + native_slot_mark(frame->handlerdata->key_field_type, &frame->key_storage); + native_slot_mark(frame->handlerdata->value_field_type, &frame->value_storage); +} + +void MapParseFrame_free(void* self) { + xfree(self); +} + +rb_data_type_t MapParseFrame_type = { + "MapParseFrame", + { MapParseFrame_mark, MapParseFrame_free, NULL }, +}; + +// Array of Ruby objects wrapping map_parse_frame_t. +// We don't allow multiple concurrent decodes, so we assume that this global +// variable is specific to the "current" decode. +VALUE map_parse_frames; + +static map_parse_frame_t* map_push_frame(VALUE map, + const map_handlerdata_t* handlerdata) { + map_parse_frame_t* frame = ALLOC(map_parse_frame_t); + frame->handlerdata = handlerdata; + frame->map = map; + native_slot_init(handlerdata->key_field_type, &frame->key_storage); + native_slot_init(handlerdata->value_field_type, &frame->value_storage); + + rb_ary_push(map_parse_frames, + TypedData_Wrap_Struct(rb_cObject, &MapParseFrame_type, frame)); + + return frame; +} + +static void map_pop_frame() { + rb_ary_pop(map_parse_frames); +} + // Handler to begin a map entry: allocates a temporary frame. This is the // 'startsubmsg' handler on the msgdef that contains the map field. static void *startmapentry_handler(void *closure, const void *hd) { @@ -266,13 +310,7 @@ static void *startmapentry_handler(void *closure, const void *hd) { const map_handlerdata_t* mapdata = hd; VALUE map_rb = DEREF(msg, mapdata->ofs, VALUE); - map_parse_frame_t* frame = ALLOC(map_parse_frame_t); - frame->map = map_rb; - - native_slot_init(mapdata->key_field_type, &frame->key_storage); - native_slot_init(mapdata->value_field_type, &frame->value_storage); - - return frame; + return map_push_frame(map_rb, mapdata); } // Handler to end a map entry: inserts the value defined during the message into @@ -298,7 +336,7 @@ static bool endmap_handler(void *closure, const void *hd, upb_status* s) { &frame->value_storage); Map_index_set(frame->map, key, value); - xfree(frame); + map_pop_frame(); return true; } @@ -737,6 +775,10 @@ VALUE Message_decode(VALUE klass, VALUE data) { msg_rb = rb_class_new_instance(0, NULL, msgklass); TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); + // We generally expect this to be clear already, but clear it in case parsing + // previously got interrupted somehow. + rb_ary_clear(map_parse_frames); + { const upb_pbdecodermethod* method = msgdef_decodermethod(desc); const upb_handlers* h = upb_pbdecodermethod_desthandlers(method); @@ -781,6 +823,10 @@ VALUE Message_decode_json(VALUE klass, VALUE data) { msg_rb = rb_class_new_instance(0, NULL, msgklass); TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); + // We generally expect this to be clear already, but clear it in case parsing + // previously got interrupted somehow. + rb_ary_clear(map_parse_frames); + { const upb_json_parsermethod* method = msgdef_jsonparsermethod(desc); stackenv se; diff --git a/ruby/ext/google/protobuf_c/protobuf.c b/ruby/ext/google/protobuf_c/protobuf.c index 7cde4aec..98963667 100644 --- a/ruby/ext/google/protobuf_c/protobuf.c +++ b/ruby/ext/google/protobuf_c/protobuf.c @@ -112,4 +112,6 @@ void Init_protobuf_c() { upb_def_to_ruby_obj_map = rb_hash_new(); rb_gc_register_address(&upb_def_to_ruby_obj_map); + map_parse_frames = rb_ary_new(); + rb_gc_register_address(&map_parse_frames); } diff --git a/ruby/ext/google/protobuf_c/protobuf.h b/ruby/ext/google/protobuf_c/protobuf.h index 08b8d516..d5ced567 100644 --- a/ruby/ext/google/protobuf_c/protobuf.h +++ b/ruby/ext/google/protobuf_c/protobuf.h @@ -166,6 +166,8 @@ extern VALUE cBuilder; extern VALUE cError; extern VALUE cParseError; +extern VALUE map_parse_frames; + // We forward-declare all of the Ruby method implementations here because we // sometimes call the methods directly across .c files, rather than going // through Ruby's method dispatching (e.g. during message parse). It's cleaner diff --git a/ruby/ext/google/protobuf_c/upb.c b/ruby/ext/google/protobuf_c/upb.c index 976a3934..544ebc04 100644 --- a/ruby/ext/google/protobuf_c/upb.c +++ b/ruby/ext/google/protobuf_c/upb.c @@ -11175,7 +11175,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); upb_sink_putstring(&subsink, sel, buf, len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&subsink, sel); + upb_sink_endstr(&p->top->sink, sel); multipart_end(p); break; } -- cgit v1.2.3 From 6e11540b329c363b614afe9dbfd92d6d1f8ba674 Mon Sep 17 00:00:00 2001 From: Jisi Liu Date: Fri, 2 Sep 2016 11:57:49 -0700 Subject: Bump version number to 3.0.2 --- configure.ac | 2 +- csharp/Google.Protobuf.Tools.nuspec | 2 +- csharp/src/Google.Protobuf/project.json | 2 +- java/core/pom.xml | 2 +- java/pom.xml | 2 +- java/util/pom.xml | 2 +- js/package.json | 2 +- protoc-artifacts/pom.xml | 2 +- python/google/protobuf/__init__.py | 2 +- ruby/google-protobuf.gemspec | 2 +- ruby/pom.xml | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) (limited to 'ruby') diff --git a/configure.ac b/configure.ac index 41887997..757f55c9 100644 --- a/configure.ac +++ b/configure.ac @@ -17,7 +17,7 @@ AC_PREREQ(2.59) # In the SVN trunk, the version should always be the next anticipated release # version with the "-pre" suffix. (We used to use "-SNAPSHOT" but this pushed # the size of one file name in the dist tarfile over the 99-char limit.) -AC_INIT([Protocol Buffers],[3.0.0],[protobuf@googlegroups.com],[protobuf]) +AC_INIT([Protocol Buffers],[3.0.2],[protobuf@googlegroups.com],[protobuf]) AM_MAINTAINER_MODE([enable]) diff --git a/csharp/Google.Protobuf.Tools.nuspec b/csharp/Google.Protobuf.Tools.nuspec index 24b4a776..ce7514b4 100644 --- a/csharp/Google.Protobuf.Tools.nuspec +++ b/csharp/Google.Protobuf.Tools.nuspec @@ -5,7 +5,7 @@ Google Protocol Buffers tools Tools for Protocol Buffers - Google's data interchange format. See project site for more info. - 3.0.0 + 3.0.2 Google Inc. protobuf-packages https://github.com/google/protobuf/blob/master/LICENSE diff --git a/csharp/src/Google.Protobuf/project.json b/csharp/src/Google.Protobuf/project.json index 9b831f12..607b65ef 100644 --- a/csharp/src/Google.Protobuf/project.json +++ b/csharp/src/Google.Protobuf/project.json @@ -1,5 +1,5 @@ { - "version": "3.0.0", + "version": "3.0.2", "title": "Google Protocol Buffers", "description": "See project site for more info.", "authors": [ "Google Inc." ], diff --git a/java/core/pom.xml b/java/core/pom.xml index 39d67818..28bc99a0 100644 --- a/java/core/pom.xml +++ b/java/core/pom.xml @@ -6,7 +6,7 @@ com.google.protobuf protobuf-parent - 3.0.0 + 3.0.2 protobuf-java diff --git a/java/pom.xml b/java/pom.xml index 3a91a0ba..ccfb229e 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -11,7 +11,7 @@ com.google.protobuf protobuf-parent - 3.0.0 + 3.0.2 pom Protocol Buffers [Parent] diff --git a/java/util/pom.xml b/java/util/pom.xml index 0d5e8e37..58cc4b30 100644 --- a/java/util/pom.xml +++ b/java/util/pom.xml @@ -6,7 +6,7 @@ com.google.protobuf protobuf-parent - 3.0.0 + 3.0.2 protobuf-java-util diff --git a/js/package.json b/js/package.json index 3f51d96b..5df7283c 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "google-protobuf", - "version": "3.0.0", + "version": "3.0.2", "description": "Protocol Buffers for JavaScript", "main": "google-protobuf.js", "files": [ diff --git a/protoc-artifacts/pom.xml b/protoc-artifacts/pom.xml index 840bc60a..c4f0a73d 100644 --- a/protoc-artifacts/pom.xml +++ b/protoc-artifacts/pom.xml @@ -10,7 +10,7 @@ com.google.protobuf protoc - 3.0.0 + 3.0.2 pom Protobuf Compiler diff --git a/python/google/protobuf/__init__.py b/python/google/protobuf/__init__.py index 6210a404..30ee6e81 100755 --- a/python/google/protobuf/__init__.py +++ b/python/google/protobuf/__init__.py @@ -30,7 +30,7 @@ # Copyright 2007 Google Inc. All Rights Reserved. -__version__ = '3.0.0' +__version__ = '3.0.2' if __name__ != '__main__': try: diff --git a/ruby/google-protobuf.gemspec b/ruby/google-protobuf.gemspec index 286d8fe3..67b15c33 100644 --- a/ruby/google-protobuf.gemspec +++ b/ruby/google-protobuf.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = "google-protobuf" - s.version = "3.0.0" + s.version = "3.0.2" s.licenses = ["BSD"] s.summary = "Protocol Buffers" s.description = "Protocol Buffers are Google's data interchange format." diff --git a/ruby/pom.xml b/ruby/pom.xml index 99e8449b..7b896137 100644 --- a/ruby/pom.xml +++ b/ruby/pom.xml @@ -86,7 +86,7 @@ com.google.protobuf protobuf-java - 3.0.0-beta-4 + 3.0.0 -- cgit v1.2.3