From cc5a39458c52b8f1fc512ec331d84a13c543bd75 Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Thu, 1 Feb 2024 00:14:24 +0300 Subject: [PATCH] Update IR IR commit: 553af9c2263c873ac1d01aa49925a4713122e8b4 --- ext/opcache/jit/ir/dynasm/dasm_arm64.h | 13 ++++++++----- ext/opcache/jit/ir/ir.c | 8 ++++---- ext/opcache/jit/ir/ir_gdb.c | 2 +- ext/opcache/jit/ir/ir_perf.c | 4 ++-- ext/opcache/jit/ir/ir_strtab.c | 6 +++--- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ext/opcache/jit/ir/dynasm/dasm_arm64.h b/ext/opcache/jit/ir/dynasm/dasm_arm64.h index cacc1130c58..1257fb2011e 100644 --- a/ext/opcache/jit/ir/dynasm/dasm_arm64.h +++ b/ext/opcache/jit/ir/dynasm/dasm_arm64.h @@ -83,6 +83,9 @@ struct dasm_State { /* The size of the core structure depends on the max. number of sections. */ #define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) +/* Perform potentially overflowing pointer operations in a way that avoids UB. */ +#define DASM_PTR_SUB(p1, off) ((void *) ((uintptr_t) (p1) - sizeof(*p1) * (uintptr_t) (off))) +#define DASM_PTR_ADD(p1, off) ((void *) ((uintptr_t) (p1) + sizeof(*p1) * (uintptr_t) (off))) /* Initialize DynASM state. */ void dasm_init(Dst_DECL, int maxsection) @@ -144,7 +147,7 @@ void dasm_setup(Dst_DECL, const void *actionlist) if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); for (i = 0; i < D->maxsection; i++) { D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].rbuf = D->sections[i].buf - D->sections[i].pos; + D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, D->sections[i].pos); D->sections[i].ofs = 0; } } @@ -429,7 +432,7 @@ int dasm_encode(Dst_DECL, void *buffer) for (secnum = 0; secnum < D->maxsection; secnum++) { dasm_Section *sec = D->sections + secnum; int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; + int *endb = DASM_PTR_ADD(sec->rbuf, sec->pos); while (b != endb) { dasm_ActList p = D->actionlist + *b++; @@ -463,15 +466,15 @@ int dasm_encode(Dst_DECL, void *buffer) cp[-1] |= ((n >> 2) & 0x03ffffff); } else if ((ins & 0x800)) { /* B.cond, CBZ, CBNZ, LDR* literal */ CK_REL((n & 3) == 0 && ((n+0x00100000) >> 21) == 0, n); - cp[-1] |= ((n << 3) & 0x00ffffe0); + cp[-1] |= (((unsigned)n << 3) & 0x00ffffe0); } else if ((ins & 0x3000) == 0x2000) { /* ADR */ CK_REL(((n+0x00100000) >> 21) == 0, n); - cp[-1] |= ((n << 3) & 0x00ffffe0) | ((n & 3) << 29); + cp[-1] |= (((unsigned)n << 3) & 0x00ffffe0) | ((n & 3) << 29); } else if ((ins & 0x3000) == 0x3000) { /* ADRP */ cp[-1] |= ((n >> 9) & 0x00ffffe0) | (((n >> 12) & 3) << 29); } else if ((ins & 0x1000)) { /* TBZ, TBNZ */ CK_REL((n & 3) == 0 && ((n+0x00008000) >> 16) == 0, n); - cp[-1] |= ((n << 3) & 0x0007ffe0); + cp[-1] |= (((unsigned)n << 3) & 0x0007ffe0); } else if ((ins & 0x8000)) { /* absolute */ cp[0] = (unsigned int)((ptrdiff_t)cp - 4 + n); cp[1] = (unsigned int)(((ptrdiff_t)cp - 4 + n) >> 32); diff --git a/ext/opcache/jit/ir/ir.c b/ext/opcache/jit/ir/ir.c index faf450c34d7..5a76384e1b3 100644 --- a/ext/opcache/jit/ir/ir.c +++ b/ext/opcache/jit/ir/ir.c @@ -187,9 +187,9 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted if (isnan(insn->val.d)) { fprintf(f, "nan"); } else { - sprintf(buf, "%g", insn->val.d); + snprintf(buf, sizeof(buf), "%g", insn->val.d); if (strtod(buf, NULL) != insn->val.d) { - sprintf(buf, "%.53e", insn->val.d); + snprintf(buf, sizeof(buf), "%.53e", insn->val.d); if (strtod(buf, NULL) != insn->val.d) { IR_ASSERT(0 && "can't format double"); } @@ -201,9 +201,9 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted if (isnan(insn->val.f)) { fprintf(f, "nan"); } else { - sprintf(buf, "%g", insn->val.f); + snprintf(buf, sizeof(buf), "%g", insn->val.f); if (strtod(buf, NULL) != insn->val.f) { - sprintf(buf, "%.24e", insn->val.f); + snprintf(buf, sizeof(buf), "%.24e", insn->val.f); if (strtod(buf, NULL) != insn->val.f) { IR_ASSERT(0 && "can't format float"); } diff --git a/ext/opcache/jit/ir/ir_gdb.c b/ext/opcache/jit/ir/ir_gdb.c index a7fa88a9a16..cf3a507d253 100644 --- a/ext/opcache/jit/ir/ir_gdb.c +++ b/ext/opcache/jit/ir/ir_gdb.c @@ -600,7 +600,7 @@ bool ir_gdb_present(void) pid = atoi(s); if (pid) { char out[1024]; - sprintf(buf, "/proc/%d/exe", (int)pid); + snprintf(buf, sizeof(buf), "/proc/%d/exe", (int)pid); if (readlink(buf, out, sizeof(out) - 1) > 0) { if (strstr(out, "gdb")) { ret = 1; diff --git a/ext/opcache/jit/ir/ir_perf.c b/ext/opcache/jit/ir/ir_perf.c index 7fcc3fcec7a..dbb689b091f 100644 --- a/ext/opcache/jit/ir/ir_perf.c +++ b/ext/opcache/jit/ir/ir_perf.c @@ -105,7 +105,7 @@ int ir_perf_jitdump_open(void) ir_elf_header elf_hdr; ir_perf_jitdump_header jit_hdr; - sprintf(filename, "/tmp/jit-%d.dump", getpid()); + snprintf(filename, sizeof(filename), "/tmp/jit-%d.dump", getpid()); if (!ir_perf_timestamp()) { return 0; } @@ -256,7 +256,7 @@ void ir_perf_map_register(const char *name, const void *start, size_t size) if (!fp) { char filename[64]; - sprintf(filename, "/tmp/perf-%d.map", getpid()); + snprintf(filename, sizeof(filename), "/tmp/perf-%d.map", getpid()); fp = fopen(filename, "w"); if (!fp) { return; diff --git a/ext/opcache/jit/ir/ir_strtab.c b/ext/opcache/jit/ir/ir_strtab.c index 7442b6ea509..476bdccef5d 100644 --- a/ext/opcache/jit/ir/ir_strtab.c +++ b/ext/opcache/jit/ir/ir_strtab.c @@ -72,15 +72,15 @@ static void ir_strtab_resize(ir_strtab *strtab) static void ir_strtab_grow_buf(ir_strtab *strtab, uint32_t len) { - size_t old = (size_t)strtab->buf; + intptr_t old = (intptr_t)strtab->buf; do { strtab->buf_size *= 2; } while (UNEXPECTED(strtab->buf_size - strtab->buf_top < len + 1)); strtab->buf = ir_mem_realloc(strtab->buf, strtab->buf_size); - if ((size_t)strtab->buf != old) { - size_t offset = (size_t)strtab->buf - old; + if ((intptr_t)strtab->buf != old) { + intptr_t offset = (intptr_t)strtab->buf - old; ir_strtab_bucket *p = (ir_strtab_bucket*)strtab->data; uint32_t i; for (i = strtab->count; i > 0; i--) {