Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit on sanitizer trap, don't recover #25

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ jobs:
- uses: actions/checkout@v3
- name: test
run: |
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_ASAN=y test
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_ASAN=y ASAN_OPTIONS="halt_on_error=1" test
linux-msan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: test
run: |
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_MSAN=y CONFIG_CLANG=y test
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_MSAN=y CONFIG_CLANG=y MSAN_OPTIONS="halt_on_error=1" test
linux-ubsan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: test
run: |
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_UBSAN=y test
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_UBSAN=y UBSAN_OPTIONS="halt_on_error=1" test

macos:
runs-on: macos-latest
Expand All @@ -69,14 +69,14 @@ jobs:
- uses: actions/checkout@v3
- name: test
run: |
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_ASAN=y test
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_ASAN=y ASAN_OPTIONS="halt_on_error=1" test
macos-ubsan:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: test
run: |
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_UBSAN=y test
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_UBSAN=y UBSAN_OPTIONS="halt_on_error=1" test

windows-mingw:
runs-on: windows-latest
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ CFLAGS+=-p
LDFLAGS+=-p
endif
ifdef CONFIG_ASAN
CFLAGS+=-fsanitize=address -fno-omit-frame-pointer
LDFLAGS+=-fsanitize=address -fno-omit-frame-pointer
CFLAGS+=-fsanitize=address -fno-sanitize-recover=all -fno-omit-frame-pointer
LDFLAGS+=-fsanitize=address -fno-sanitize-recover=all -fno-omit-frame-pointer
endif
ifdef CONFIG_MSAN
CFLAGS+=-fsanitize=memory -fno-omit-frame-pointer
LDFLAGS+=-fsanitize=memory -fno-omit-frame-pointer
CFLAGS+=-fsanitize=memory -fno-sanitize-recover=all -fno-omit-frame-pointer
LDFLAGS+=-fsanitize=memory -fno-sanitize-recover=all -fno-omit-frame-pointer
endif
ifdef CONFIG_UBSAN
CFLAGS+=-fsanitize=undefined -fno-omit-frame-pointer
LDFLAGS+=-fsanitize=undefined -fno-omit-frame-pointer
CFLAGS+=-fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer
LDFLAGS+=-fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer
endif
ifdef CONFIG_WIN32
LDEXPORT=
Expand Down
4 changes: 0 additions & 4 deletions libunicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,7 @@ int lre_case_conv(uint32_t *res, uint32_t c, int conv_type)

static uint32_t get_le24(const uint8_t *ptr)
{
#if defined(__x86__) || defined(__x86_64__)
return *(uint16_t *)ptr | (ptr[2] << 16);
#else
return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16);
#endif
}

#define UNICODE_INDEX_BLOCK_LEN 32
Expand Down
45 changes: 43 additions & 2 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10797,7 +10797,8 @@ static int JS_ToInt64Free(JSContext *ctx, int64_t *pres, JSValue val)
ret = v << ((e - 1023) - 52);
/* take the sign into account */
if (u.u64 >> 63)
ret = -ret;
if (ret != INT64_MIN)
ret = -ret;
} else {
ret = 0; /* also handles NaN and +inf */
}
Expand Down Expand Up @@ -10872,7 +10873,8 @@ static int JS_ToInt32Free(JSContext *ctx, int32_t *pres, JSValue val)
ret = v >> 32;
/* take the sign into account */
if (u.u64 >> 63)
ret = -ret;
if (ret != INT32_MIN)
ret = -ret;
} else {
ret = 0; /* also handles NaN and +inf */
}
Expand Down Expand Up @@ -11968,6 +11970,45 @@ static double js_pow(double a, double b)
}
}

// Special care is taken to not invoke UB when checking if the result fits
// in an int32_t. Leans on the fact that the input is integral if the lower
// 52 bits of the equation 2**e * (f + 2**52) are zero.
static BOOL float_is_int32(double d)
{
uint64_t u, m, e, f;
JSFloat64Union t;

t.d = d;
u = t.u64;

// special case -0
m = 1ull << 63;
if (u == m)
return FALSE;

e = (u >> 52) & 0x7FF;
if (e > 0)
e -= 1023;

// too large, nan or inf?
if (e > 30)
return FALSE;

// fractional or subnormal if low bits are non-zero
f = 0xFFFFFFFFFFFFFull & u;
m = 0xFFFFFFFFFFFFFull >> e;
return 0 == (f & m);
}

JSValue JS_NewFloat64(JSContext *ctx, double d)
{
if (float_is_int32(d)) {
return JS_MKVAL(JS_TAG_INT, (int32_t)d);
} else {
return __JS_NewFloat64(ctx, d);
}
}

#ifdef CONFIG_BIGNUM

JSValue JS_NewBigInt64_1(JSContext *ctx, int64_t v)
Expand Down
22 changes: 1 addition & 21 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,30 +539,10 @@ static js_force_inline JSValue JS_NewUint32(JSContext *ctx, uint32_t val)
return v;
}

JSValue JS_NewFloat64(JSContext *ctx, double d);
JSValue JS_NewBigInt64(JSContext *ctx, int64_t v);
JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v);

static js_force_inline JSValue JS_NewFloat64(JSContext *ctx, double d)
{
JSValue v;
int32_t val;
union {
double d;
uint64_t u;
} u, t;
u.d = d;
val = (int32_t)d;
t.d = val;
/* -0 cannot be represented as integer, so we compare the bit
representation */
if (u.u == t.u) {
v = JS_MKVAL(JS_TAG_INT, val);
} else {
v = __JS_NewFloat64(ctx, d);
}
return v;
}

static inline JS_BOOL JS_IsNumber(JSValueConst v)
{
int tag = JS_VALUE_GET_TAG(v);
Expand Down
4 changes: 4 additions & 0 deletions tests/test_builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ function test_number()
assert(+" 123 ", 123);
assert(+"0b111", 7);
assert(+"0o123", 83);
assert(parseFloat("2147483647"), 2147483647);
assert(parseFloat("2147483648"), 2147483648);
assert(parseFloat("-2147483647"), -2147483647);
assert(parseFloat("-2147483648"), -2147483648);
assert(parseFloat("0x1234"), 0);
assert(parseFloat("Infinity"), Infinity);
assert(parseFloat("-Infinity"), -Infinity);
Expand Down