From 4caff42641d444a54991347c53a4d6a6cde5aa9d Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Mon, 6 Mar 2023 20:16:41 +0100 Subject: [PATCH] Fix invalid pointer arithmetics Compiling with -fsanitize=undefined complains about invalid pointer arithmetics on a null pointer. --- dynasm/dasm_arm.h | 7 +++++-- dynasm/dasm_arm64.h | 7 +++++-- dynasm/dasm_mips.h | 7 +++++-- dynasm/dasm_ppc.h | 7 +++++-- dynasm/dasm_x86.h | 7 +++++-- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/dynasm/dasm_arm.h b/dynasm/dasm_arm.h index fbfebee0..7bfaae35 100644 --- a/dynasm/dasm_arm.h +++ b/dynasm/dasm_arm.h @@ -81,6 +81,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) @@ -100,7 +103,7 @@ void dasm_init(Dst_DECL, int maxsection) D->maxsection = maxsection; for (i = 0; i < maxsection; i++) { D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); + D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, DASM_SEC2POS(i)); D->sections[i].bsize = 0; D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ } @@ -353,7 +356,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++; diff --git a/dynasm/dasm_arm64.h b/dynasm/dasm_arm64.h index 47c9c37d..f9e38c8c 100644 --- a/dynasm/dasm_arm64.h +++ b/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) @@ -102,7 +105,7 @@ void dasm_init(Dst_DECL, int maxsection) D->maxsection = maxsection; for (i = 0; i < maxsection; i++) { D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); + D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, DASM_SEC2POS(i)); D->sections[i].bsize = 0; D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ } @@ -425,7 +428,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++; diff --git a/dynasm/dasm_mips.h b/dynasm/dasm_mips.h index 3e99a005..eeb58409 100644 --- a/dynasm/dasm_mips.h +++ b/dynasm/dasm_mips.h @@ -80,6 +80,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) @@ -99,7 +102,7 @@ void dasm_init(Dst_DECL, int maxsection) D->maxsection = maxsection; for (i = 0; i < maxsection; i++) { D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); + D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, DASM_SEC2POS(i)); D->sections[i].bsize = 0; D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ } @@ -331,7 +334,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++; diff --git a/dynasm/dasm_ppc.h b/dynasm/dasm_ppc.h index fdb89bce..0f188c18 100644 --- a/dynasm/dasm_ppc.h +++ b/dynasm/dasm_ppc.h @@ -80,6 +80,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) @@ -99,7 +102,7 @@ void dasm_init(Dst_DECL, int maxsection) D->maxsection = maxsection; for (i = 0; i < maxsection; i++) { D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); + D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, DASM_SEC2POS(i)); D->sections[i].bsize = 0; D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ } @@ -335,7 +338,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++; diff --git a/dynasm/dasm_x86.h b/dynasm/dasm_x86.h index f0327302..c5e7ba13 100644 --- a/dynasm/dasm_x86.h +++ b/dynasm/dasm_x86.h @@ -79,6 +79,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) @@ -98,7 +101,7 @@ void dasm_init(Dst_DECL, int maxsection) D->maxsection = maxsection; for (i = 0; i < maxsection; i++) { D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); + D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, DASM_SEC2POS(i)); D->sections[i].bsize = 0; D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ } @@ -397,7 +400,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++;