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

Enable ASan en UBSan sanitizers on macOS CI #8

Merged
merged 2 commits into from
Nov 3, 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
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ jobs:
- name: test
run: |
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y test
macos-asan:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: test
run: |
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_ASAN=y 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
46 changes: 24 additions & 22 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11377,20 +11377,20 @@ static int js_ecvt(double d, int n_digits, int *decpt, int *sign, char *buf,
return n_digits;
}

static int js_fcvt1(char *buf, int buf_size, double d, int n_digits,
static int js_fcvt1(char (*buf)[JS_DTOA_BUF_SIZE], double d, int n_digits,
int rounding_mode)
{
int n;
if (rounding_mode != FE_TONEAREST)
fesetround(rounding_mode);
n = snprintf(buf, buf_size, "%.*f", n_digits, d);
n = snprintf(*buf, sizeof(*buf), "%.*f", n_digits, d);
if (rounding_mode != FE_TONEAREST)
fesetround(FE_TONEAREST);
assert(n < buf_size);
assert(n < sizeof(*buf));
return n;
}

static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
static void js_fcvt(char (*buf)[JS_DTOA_BUF_SIZE], double d, int n_digits)
{
int rounding_mode;
rounding_mode = FE_TONEAREST;
Expand All @@ -11404,12 +11404,12 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
zero (RNDNA), but in printf the "ties" case is not specified
(for example it is RNDN for glibc, RNDNA for Windows), so we
must round manually. */
n1 = js_fcvt1(buf1, sizeof(buf1), d, n_digits + 1, FE_TONEAREST);
n1 = js_fcvt1(&buf1, d, n_digits + 1, FE_TONEAREST);
rounding_mode = FE_TONEAREST;
/* XXX: could use 2 digits to reduce the average running time */
if (buf1[n1 - 1] == '5') {
n1 = js_fcvt1(buf1, sizeof(buf1), d, n_digits + 1, FE_DOWNWARD);
n2 = js_fcvt1(buf2, sizeof(buf2), d, n_digits + 1, FE_UPWARD);
n1 = js_fcvt1(&buf1, d, n_digits + 1, FE_DOWNWARD);
n2 = js_fcvt1(&buf2, d, n_digits + 1, FE_UPWARD);
if (n1 == n2 && memcmp(buf1, buf2, n1) == 0) {
/* exact result: round away from zero */
if (buf1[0] == '-')
Expand All @@ -11420,7 +11420,7 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
}
}
#endif /* CONFIG_PRINTF_RNDN */
js_fcvt1(buf, buf_size, d, n_digits, rounding_mode);
js_fcvt1(buf, d, n_digits, rounding_mode);
}

/* radix != 10 is only supported with flags = JS_DTOA_VAR_FORMAT */
Expand All @@ -11436,18 +11436,18 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
/* XXX: slow and maybe not fully correct. Use libbf when it is fast enough.
XXX: radix != 10 is only supported for small integers
*/
static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
static void js_dtoa1(char (*buf)[JS_DTOA_BUF_SIZE], double d,
int radix, int n_digits, int flags)
{
char *q;

if (!isfinite(d)) {
if (isnan(d)) {
strcpy(buf, "NaN");
pstrcpy(*buf, sizeof(*buf), "NaN");
} else if (d < 0) {
pstrcpy(*buf, sizeof(*buf), "-Infinity");
} else {
q = buf;
if (d < 0)
*q++ = '-';
strcpy(q, "Infinity");
pstrcpy(*buf, sizeof(*buf), "Infinity");
}
} else if (flags == JS_DTOA_VAR_FORMAT) {
int64_t i64;
Expand All @@ -11457,12 +11457,12 @@ static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
goto generic_conv;
/* fast path for integers */
ptr = i64toa(buf1 + sizeof(buf1), i64, radix);
strcpy(buf, ptr);
pstrcpy(*buf, sizeof(*buf), ptr);
} else {
if (d == 0.0)
d = 0.0; /* convert -0 to 0 */
if (flags == JS_DTOA_FRAC_FORMAT) {
js_fcvt(buf, JS_DTOA_BUF_SIZE, d, n_digits);
js_fcvt(buf, d, n_digits);
} else {
char buf1[JS_DTOA_BUF_SIZE];
int sign, decpt, k, n, i, p, n_max;
Expand All @@ -11477,7 +11477,7 @@ static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
/* the number has k digits (k >= 1) */
k = js_ecvt(d, n_digits, &decpt, &sign, buf1, is_fixed);
n = decpt; /* d=10^(n-k)*(buf1) i.e. d= < x.yyyy 10^(n-1) */
q = buf;
q = *buf;
if (sign)
*q++ = '-';
if (flags & JS_DTOA_FORCE_EXP)
Expand Down Expand Up @@ -11519,7 +11519,7 @@ static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
p = n - 1;
if (p >= 0)
*q++ = '+';
sprintf(q, "%d", p);
snprintf(q, *buf + sizeof(*buf) - q, "%d", p);
}
}
}
Expand All @@ -11529,7 +11529,7 @@ static JSValue js_dtoa(JSContext *ctx,
double d, int radix, int n_digits, int flags)
{
char buf[JS_DTOA_BUF_SIZE];
js_dtoa1(buf, d, radix, n_digits, flags);
js_dtoa1(&buf, d, radix, n_digits, flags);
return JS_NewString(ctx, buf);
}

Expand Down Expand Up @@ -27253,6 +27253,7 @@ static char *js_default_module_normalize_name(JSContext *ctx,
{
char *filename, *p;
const char *r;
int cap;
int len;

if (name[0] != '.') {
Expand All @@ -27266,7 +27267,8 @@ static char *js_default_module_normalize_name(JSContext *ctx,
else
len = 0;

filename = js_malloc(ctx, len + strlen(name) + 1 + 1);
cap = len + strlen(name) + 1 + 1;
filename = js_malloc(ctx, cap);
if (!filename)
return NULL;
memcpy(filename, base_name, len);
Expand Down Expand Up @@ -27298,8 +27300,8 @@ static char *js_default_module_normalize_name(JSContext *ctx,
}
}
if (filename[0] != '\0')
strcat(filename, "/");
strcat(filename, r);
pstrcat(filename, cap, "/");
pstrcat(filename, cap, r);
// printf("normalize: %s %s -> %s\n", base_name, name, filename);
return filename;
}
Expand Down