Skip to content

Commit cc5a394

Browse files
committed
Update IR
IR commit: 553af9c2263c873ac1d01aa49925a4713122e8b4
1 parent 2b629f0 commit cc5a394

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

ext/opcache/jit/ir/dynasm/dasm_arm64.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ struct dasm_State {
8383
/* The size of the core structure depends on the max. number of sections. */
8484
#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
8585

86+
/* Perform potentially overflowing pointer operations in a way that avoids UB. */
87+
#define DASM_PTR_SUB(p1, off) ((void *) ((uintptr_t) (p1) - sizeof(*p1) * (uintptr_t) (off)))
88+
#define DASM_PTR_ADD(p1, off) ((void *) ((uintptr_t) (p1) + sizeof(*p1) * (uintptr_t) (off)))
8689

8790
/* Initialize DynASM state. */
8891
void dasm_init(Dst_DECL, int maxsection)
@@ -144,7 +147,7 @@ void dasm_setup(Dst_DECL, const void *actionlist)
144147
if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
145148
for (i = 0; i < D->maxsection; i++) {
146149
D->sections[i].pos = DASM_SEC2POS(i);
147-
D->sections[i].rbuf = D->sections[i].buf - D->sections[i].pos;
150+
D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, D->sections[i].pos);
148151
D->sections[i].ofs = 0;
149152
}
150153
}
@@ -429,7 +432,7 @@ int dasm_encode(Dst_DECL, void *buffer)
429432
for (secnum = 0; secnum < D->maxsection; secnum++) {
430433
dasm_Section *sec = D->sections + secnum;
431434
int *b = sec->buf;
432-
int *endb = sec->rbuf + sec->pos;
435+
int *endb = DASM_PTR_ADD(sec->rbuf, sec->pos);
433436

434437
while (b != endb) {
435438
dasm_ActList p = D->actionlist + *b++;
@@ -463,15 +466,15 @@ int dasm_encode(Dst_DECL, void *buffer)
463466
cp[-1] |= ((n >> 2) & 0x03ffffff);
464467
} else if ((ins & 0x800)) { /* B.cond, CBZ, CBNZ, LDR* literal */
465468
CK_REL((n & 3) == 0 && ((n+0x00100000) >> 21) == 0, n);
466-
cp[-1] |= ((n << 3) & 0x00ffffe0);
469+
cp[-1] |= (((unsigned)n << 3) & 0x00ffffe0);
467470
} else if ((ins & 0x3000) == 0x2000) { /* ADR */
468471
CK_REL(((n+0x00100000) >> 21) == 0, n);
469-
cp[-1] |= ((n << 3) & 0x00ffffe0) | ((n & 3) << 29);
472+
cp[-1] |= (((unsigned)n << 3) & 0x00ffffe0) | ((n & 3) << 29);
470473
} else if ((ins & 0x3000) == 0x3000) { /* ADRP */
471474
cp[-1] |= ((n >> 9) & 0x00ffffe0) | (((n >> 12) & 3) << 29);
472475
} else if ((ins & 0x1000)) { /* TBZ, TBNZ */
473476
CK_REL((n & 3) == 0 && ((n+0x00008000) >> 16) == 0, n);
474-
cp[-1] |= ((n << 3) & 0x0007ffe0);
477+
cp[-1] |= (((unsigned)n << 3) & 0x0007ffe0);
475478
} else if ((ins & 0x8000)) { /* absolute */
476479
cp[0] = (unsigned int)((ptrdiff_t)cp - 4 + n);
477480
cp[1] = (unsigned int)(((ptrdiff_t)cp - 4 + n) >> 32);

ext/opcache/jit/ir/ir.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted
187187
if (isnan(insn->val.d)) {
188188
fprintf(f, "nan");
189189
} else {
190-
sprintf(buf, "%g", insn->val.d);
190+
snprintf(buf, sizeof(buf), "%g", insn->val.d);
191191
if (strtod(buf, NULL) != insn->val.d) {
192-
sprintf(buf, "%.53e", insn->val.d);
192+
snprintf(buf, sizeof(buf), "%.53e", insn->val.d);
193193
if (strtod(buf, NULL) != insn->val.d) {
194194
IR_ASSERT(0 && "can't format double");
195195
}
@@ -201,9 +201,9 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted
201201
if (isnan(insn->val.f)) {
202202
fprintf(f, "nan");
203203
} else {
204-
sprintf(buf, "%g", insn->val.f);
204+
snprintf(buf, sizeof(buf), "%g", insn->val.f);
205205
if (strtod(buf, NULL) != insn->val.f) {
206-
sprintf(buf, "%.24e", insn->val.f);
206+
snprintf(buf, sizeof(buf), "%.24e", insn->val.f);
207207
if (strtod(buf, NULL) != insn->val.f) {
208208
IR_ASSERT(0 && "can't format float");
209209
}

ext/opcache/jit/ir/ir_gdb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ bool ir_gdb_present(void)
600600
pid = atoi(s);
601601
if (pid) {
602602
char out[1024];
603-
sprintf(buf, "/proc/%d/exe", (int)pid);
603+
snprintf(buf, sizeof(buf), "/proc/%d/exe", (int)pid);
604604
if (readlink(buf, out, sizeof(out) - 1) > 0) {
605605
if (strstr(out, "gdb")) {
606606
ret = 1;

ext/opcache/jit/ir/ir_perf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int ir_perf_jitdump_open(void)
105105
ir_elf_header elf_hdr;
106106
ir_perf_jitdump_header jit_hdr;
107107

108-
sprintf(filename, "/tmp/jit-%d.dump", getpid());
108+
snprintf(filename, sizeof(filename), "/tmp/jit-%d.dump", getpid());
109109
if (!ir_perf_timestamp()) {
110110
return 0;
111111
}
@@ -256,7 +256,7 @@ void ir_perf_map_register(const char *name, const void *start, size_t size)
256256
if (!fp) {
257257
char filename[64];
258258

259-
sprintf(filename, "/tmp/perf-%d.map", getpid());
259+
snprintf(filename, sizeof(filename), "/tmp/perf-%d.map", getpid());
260260
fp = fopen(filename, "w");
261261
if (!fp) {
262262
return;

ext/opcache/jit/ir/ir_strtab.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ static void ir_strtab_resize(ir_strtab *strtab)
7272

7373
static void ir_strtab_grow_buf(ir_strtab *strtab, uint32_t len)
7474
{
75-
size_t old = (size_t)strtab->buf;
75+
intptr_t old = (intptr_t)strtab->buf;
7676

7777
do {
7878
strtab->buf_size *= 2;
7979
} while (UNEXPECTED(strtab->buf_size - strtab->buf_top < len + 1));
8080

8181
strtab->buf = ir_mem_realloc(strtab->buf, strtab->buf_size);
82-
if ((size_t)strtab->buf != old) {
83-
size_t offset = (size_t)strtab->buf - old;
82+
if ((intptr_t)strtab->buf != old) {
83+
intptr_t offset = (intptr_t)strtab->buf - old;
8484
ir_strtab_bucket *p = (ir_strtab_bucket*)strtab->data;
8585
uint32_t i;
8686
for (i = strtab->count; i > 0; i--) {

0 commit comments

Comments
 (0)