Skip to content

Commit 4e07b63

Browse files
authored
Merge pull request #6618 from TeBoring/3.10.x-cherrypick
Cherry Pick (#6614)
2 parents 56bf637 + 398d37f commit 4e07b63

File tree

4 files changed

+32
-152
lines changed

4 files changed

+32
-152
lines changed

php/ext/google/protobuf/upb.c

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,8 @@ static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) {
15361536
char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
15371537
if (str == NULL) return 0;
15381538
memcpy(str, &len, sizeof(uint32_t));
1539-
memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len + 1);
1539+
memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len);
1540+
str[sizeof(uint32_t) + k2.str.len] = '\0';
15401541
return (uintptr_t)str;
15411542
}
15421543

@@ -6309,12 +6310,10 @@ static void set_bytecode_handlers(mgroup *g) {
63096310

63106311
/* TODO(haberman): allow this to be constructed for an arbitrary set of dest
63116312
* handlers and other mgroups (but verify we have a transitive closure). */
6312-
const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy) {
6313+
const mgroup *mgroup_new(const upb_handlers *dest, bool lazy) {
63136314
mgroup *g;
63146315
compiler *c;
63156316

6316-
UPB_UNUSED(allowjit);
6317-
63186317
g = newgroup();
63196318
c = newcompiler(g, lazy);
63206319
find_methods(c, dest);
@@ -6359,7 +6358,6 @@ upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) {
63596358
if (!c) return NULL;
63606359

63616360
c->dest = dest;
6362-
c->allow_jit = true;
63636361
c->lazy = false;
63646362

63656363
c->arena = upb_arena_new();
@@ -6369,29 +6367,19 @@ upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) {
63696367
}
63706368

63716369
void upb_pbcodecache_free(upb_pbcodecache *c) {
6372-
size_t i;
6370+
upb_inttable_iter i;
63736371

6374-
for (i = 0; i < upb_inttable_count(&c->groups); i++) {
6375-
upb_value v;
6376-
bool ok = upb_inttable_lookup(&c->groups, i, &v);
6377-
UPB_ASSERT(ok);
6378-
freegroup((void*)upb_value_getconstptr(v));
6372+
upb_inttable_begin(&i, &c->groups);
6373+
for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
6374+
upb_value val = upb_inttable_iter_value(&i);
6375+
freegroup((void*)upb_value_getconstptr(val));
63796376
}
63806377

63816378
upb_inttable_uninit(&c->groups);
63826379
upb_arena_free(c->arena);
63836380
upb_gfree(c);
63846381
}
63856382

6386-
bool upb_pbcodecache_allowjit(const upb_pbcodecache *c) {
6387-
return c->allow_jit;
6388-
}
6389-
6390-
void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow) {
6391-
UPB_ASSERT(upb_inttable_count(&c->groups) == 0);
6392-
c->allow_jit = allow;
6393-
}
6394-
63956383
void upb_pbdecodermethodopts_setlazy(upb_pbcodecache *c, bool lazy) {
63966384
UPB_ASSERT(upb_inttable_count(&c->groups) == 0);
63976385
c->lazy = lazy;
@@ -6404,11 +6392,14 @@ const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c,
64046392
const upb_handlers *h;
64056393
const mgroup *g;
64066394

6407-
/* Right now we build a new DecoderMethod every time.
6408-
* TODO(haberman): properly cache methods by their true key. */
64096395
h = upb_handlercache_get(c->dest, md);
6410-
g = mgroup_new(h, c->allow_jit, c->lazy);
6411-
upb_inttable_push(&c->groups, upb_value_constptr(g));
6396+
if (upb_inttable_lookupptr(&c->groups, md, &v)) {
6397+
g = upb_value_getconstptr(v);
6398+
} else {
6399+
g = mgroup_new(h, c->lazy);
6400+
ok = upb_inttable_insertptr(&c->groups, md, upb_value_constptr(g));
6401+
UPB_ASSERT(ok);
6402+
}
64126403

64136404
ok = upb_inttable_lookupptr(&g->methods, h, &v);
64146405
UPB_ASSERT(ok);
@@ -6490,16 +6481,6 @@ static size_t stacksize(upb_pbdecoder *d, size_t entries) {
64906481
static size_t callstacksize(upb_pbdecoder *d, size_t entries) {
64916482
UPB_UNUSED(d);
64926483

6493-
#ifdef UPB_USE_JIT_X64
6494-
if (d->method_->is_native_) {
6495-
/* Each native stack frame needs two pointers, plus we need a few frames for
6496-
* the enter/exit trampolines. */
6497-
size_t ret = entries * sizeof(void*) * 2;
6498-
ret += sizeof(void*) * 10;
6499-
return ret;
6500-
}
6501-
#endif
6502-
65036484
return entries * sizeof(uint32_t*);
65046485
}
65056486

@@ -7315,17 +7296,6 @@ void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint) {
73157296
return d;
73167297
}
73177298

7318-
void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint) {
7319-
upb_pbdecoder *d = closure;
7320-
UPB_UNUSED(hd);
7321-
UPB_UNUSED(size_hint);
7322-
d->top->end_ofs = UINT64_MAX;
7323-
d->bufstart_ofs = 0;
7324-
d->call_len = 0;
7325-
d->skip = 0;
7326-
return d;
7327-
}
7328-
73297299
bool upb_pbdecoder_end(void *closure, const void *handler_data) {
73307300
upb_pbdecoder *d = closure;
73317301
const upb_pbdecodermethod *method = handler_data;
@@ -7351,14 +7321,6 @@ bool upb_pbdecoder_end(void *closure, const void *handler_data) {
73517321
end = offset(d);
73527322
d->top->end_ofs = end;
73537323

7354-
#ifdef UPB_USE_JIT_X64
7355-
if (method->is_native_) {
7356-
const mgroup *group = (const mgroup*)method->group;
7357-
if (d->top != d->stack)
7358-
d->stack->end_ofs = 0;
7359-
group->jit_code(closure, method->code_base.ptr, &dummy, 0, NULL);
7360-
} else
7361-
#endif
73627324
{
73637325
const uint32_t *p = d->pc;
73647326
d->stack->end_ofs = end;

php/ext/google/protobuf/upb.h

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6343,7 +6343,7 @@ struct upb_pbcodecache {
63436343
bool allow_jit;
63446344
bool lazy;
63456345

6346-
/* Array of mgroups. */
6346+
/* Map of upb_msgdef -> mgroup. */
63476347
upb_inttable groups;
63486348
};
63496349

@@ -6359,15 +6359,6 @@ typedef struct {
63596359
/* The bytecode for our methods, if any exists. Owned by us. */
63606360
uint32_t *bytecode;
63616361
uint32_t *bytecode_end;
6362-
6363-
#ifdef UPB_USE_JIT_X64
6364-
/* JIT-generated machine code, if any. */
6365-
upb_string_handlerfunc *jit_code;
6366-
/* The size of the jit_code (required to munmap()). */
6367-
size_t jit_size;
6368-
char *debug_info;
6369-
void *dl;
6370-
#endif
63716362
} mgroup;
63726363

63736364
/* The maximum that any submessages can be nested. Matches proto2's limit.
@@ -6478,19 +6469,10 @@ struct upb_pbdecoder {
64786469
size_t stack_size;
64796470

64806471
upb_status *status;
6481-
6482-
#ifdef UPB_USE_JIT_X64
6483-
/* Used momentarily by the generated code to store a value while a user
6484-
* function is called. */
6485-
uint32_t tmp_len;
6486-
6487-
const void *saved_rsp;
6488-
#endif
64896472
};
64906473

64916474
/* Decoder entry points; used as handlers. */
64926475
void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
6493-
void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
64946476
size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
64956477
size_t size, const upb_bufhandle *handle);
64966478
bool upb_pbdecoder_end(void *closure, const void *handler_data);
@@ -6514,10 +6496,6 @@ extern const char *kPbDecoderSubmessageTooLong;
65146496
/* Access to decoderplan members needed by the decoder. */
65156497
const char *upb_pbdecoder_getopname(unsigned int op);
65166498

6517-
/* JIT codegen entry point. */
6518-
void upb_pbdecoder_jit(mgroup *group);
6519-
void upb_pbdecoder_freejit(mgroup *group);
6520-
65216499
/* A special label that means "do field dispatch for this message and branch to
65226500
* wherever that takes you." */
65236501
#define LABEL_DISPATCH 0

ruby/ext/google/protobuf_c/upb.c

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,8 @@ static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) {
15361536
char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
15371537
if (str == NULL) return 0;
15381538
memcpy(str, &len, sizeof(uint32_t));
1539-
memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len + 1);
1539+
memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len);
1540+
str[sizeof(uint32_t) + k2.str.len] = '\0';
15401541
return (uintptr_t)str;
15411542
}
15421543

@@ -6309,12 +6310,10 @@ static void set_bytecode_handlers(mgroup *g) {
63096310

63106311
/* TODO(haberman): allow this to be constructed for an arbitrary set of dest
63116312
* handlers and other mgroups (but verify we have a transitive closure). */
6312-
const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy) {
6313+
const mgroup *mgroup_new(const upb_handlers *dest, bool lazy) {
63136314
mgroup *g;
63146315
compiler *c;
63156316

6316-
UPB_UNUSED(allowjit);
6317-
63186317
g = newgroup();
63196318
c = newcompiler(g, lazy);
63206319
find_methods(c, dest);
@@ -6359,7 +6358,6 @@ upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) {
63596358
if (!c) return NULL;
63606359

63616360
c->dest = dest;
6362-
c->allow_jit = true;
63636361
c->lazy = false;
63646362

63656363
c->arena = upb_arena_new();
@@ -6369,29 +6367,19 @@ upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) {
63696367
}
63706368

63716369
void upb_pbcodecache_free(upb_pbcodecache *c) {
6372-
size_t i;
6370+
upb_inttable_iter i;
63736371

6374-
for (i = 0; i < upb_inttable_count(&c->groups); i++) {
6375-
upb_value v;
6376-
bool ok = upb_inttable_lookup(&c->groups, i, &v);
6377-
UPB_ASSERT(ok);
6378-
freegroup((void*)upb_value_getconstptr(v));
6372+
upb_inttable_begin(&i, &c->groups);
6373+
for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
6374+
upb_value val = upb_inttable_iter_value(&i);
6375+
freegroup((void*)upb_value_getconstptr(val));
63796376
}
63806377

63816378
upb_inttable_uninit(&c->groups);
63826379
upb_arena_free(c->arena);
63836380
upb_gfree(c);
63846381
}
63856382

6386-
bool upb_pbcodecache_allowjit(const upb_pbcodecache *c) {
6387-
return c->allow_jit;
6388-
}
6389-
6390-
void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow) {
6391-
UPB_ASSERT(upb_inttable_count(&c->groups) == 0);
6392-
c->allow_jit = allow;
6393-
}
6394-
63956383
void upb_pbdecodermethodopts_setlazy(upb_pbcodecache *c, bool lazy) {
63966384
UPB_ASSERT(upb_inttable_count(&c->groups) == 0);
63976385
c->lazy = lazy;
@@ -6404,11 +6392,14 @@ const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c,
64046392
const upb_handlers *h;
64056393
const mgroup *g;
64066394

6407-
/* Right now we build a new DecoderMethod every time.
6408-
* TODO(haberman): properly cache methods by their true key. */
64096395
h = upb_handlercache_get(c->dest, md);
6410-
g = mgroup_new(h, c->allow_jit, c->lazy);
6411-
upb_inttable_push(&c->groups, upb_value_constptr(g));
6396+
if (upb_inttable_lookupptr(&c->groups, md, &v)) {
6397+
g = upb_value_getconstptr(v);
6398+
} else {
6399+
g = mgroup_new(h, c->lazy);
6400+
ok = upb_inttable_insertptr(&c->groups, md, upb_value_constptr(g));
6401+
UPB_ASSERT(ok);
6402+
}
64126403

64136404
ok = upb_inttable_lookupptr(&g->methods, h, &v);
64146405
UPB_ASSERT(ok);
@@ -6490,16 +6481,6 @@ static size_t stacksize(upb_pbdecoder *d, size_t entries) {
64906481
static size_t callstacksize(upb_pbdecoder *d, size_t entries) {
64916482
UPB_UNUSED(d);
64926483

6493-
#ifdef UPB_USE_JIT_X64
6494-
if (d->method_->is_native_) {
6495-
/* Each native stack frame needs two pointers, plus we need a few frames for
6496-
* the enter/exit trampolines. */
6497-
size_t ret = entries * sizeof(void*) * 2;
6498-
ret += sizeof(void*) * 10;
6499-
return ret;
6500-
}
6501-
#endif
6502-
65036484
return entries * sizeof(uint32_t*);
65046485
}
65056486

@@ -7315,17 +7296,6 @@ void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint) {
73157296
return d;
73167297
}
73177298

7318-
void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint) {
7319-
upb_pbdecoder *d = closure;
7320-
UPB_UNUSED(hd);
7321-
UPB_UNUSED(size_hint);
7322-
d->top->end_ofs = UINT64_MAX;
7323-
d->bufstart_ofs = 0;
7324-
d->call_len = 0;
7325-
d->skip = 0;
7326-
return d;
7327-
}
7328-
73297299
bool upb_pbdecoder_end(void *closure, const void *handler_data) {
73307300
upb_pbdecoder *d = closure;
73317301
const upb_pbdecodermethod *method = handler_data;
@@ -7351,14 +7321,6 @@ bool upb_pbdecoder_end(void *closure, const void *handler_data) {
73517321
end = offset(d);
73527322
d->top->end_ofs = end;
73537323

7354-
#ifdef UPB_USE_JIT_X64
7355-
if (method->is_native_) {
7356-
const mgroup *group = (const mgroup*)method->group;
7357-
if (d->top != d->stack)
7358-
d->stack->end_ofs = 0;
7359-
group->jit_code(closure, method->code_base.ptr, &dummy, 0, NULL);
7360-
} else
7361-
#endif
73627324
{
73637325
const uint32_t *p = d->pc;
73647326
d->stack->end_ofs = end;

ruby/ext/google/protobuf_c/upb.h

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6336,7 +6336,7 @@ struct upb_pbcodecache {
63366336
bool allow_jit;
63376337
bool lazy;
63386338

6339-
/* Array of mgroups. */
6339+
/* Map of upb_msgdef -> mgroup. */
63406340
upb_inttable groups;
63416341
};
63426342

@@ -6352,15 +6352,6 @@ typedef struct {
63526352
/* The bytecode for our methods, if any exists. Owned by us. */
63536353
uint32_t *bytecode;
63546354
uint32_t *bytecode_end;
6355-
6356-
#ifdef UPB_USE_JIT_X64
6357-
/* JIT-generated machine code, if any. */
6358-
upb_string_handlerfunc *jit_code;
6359-
/* The size of the jit_code (required to munmap()). */
6360-
size_t jit_size;
6361-
char *debug_info;
6362-
void *dl;
6363-
#endif
63646355
} mgroup;
63656356

63666357
/* The maximum that any submessages can be nested. Matches proto2's limit.
@@ -6471,19 +6462,10 @@ struct upb_pbdecoder {
64716462
size_t stack_size;
64726463

64736464
upb_status *status;
6474-
6475-
#ifdef UPB_USE_JIT_X64
6476-
/* Used momentarily by the generated code to store a value while a user
6477-
* function is called. */
6478-
uint32_t tmp_len;
6479-
6480-
const void *saved_rsp;
6481-
#endif
64826465
};
64836466

64846467
/* Decoder entry points; used as handlers. */
64856468
void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
6486-
void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
64876469
size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
64886470
size_t size, const upb_bufhandle *handle);
64896471
bool upb_pbdecoder_end(void *closure, const void *handler_data);
@@ -6507,10 +6489,6 @@ extern const char *kPbDecoderSubmessageTooLong;
65076489
/* Access to decoderplan members needed by the decoder. */
65086490
const char *upb_pbdecoder_getopname(unsigned int op);
65096491

6510-
/* JIT codegen entry point. */
6511-
void upb_pbdecoder_jit(mgroup *group);
6512-
void upb_pbdecoder_freejit(mgroup *group);
6513-
65146492
/* A special label that means "do field dispatch for this message and branch to
65156493
* wherever that takes you." */
65166494
#define LABEL_DISPATCH 0

0 commit comments

Comments
 (0)