From 7023bffa8b5a88c4ad9d4a30153f1a6684ac94ae Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Mon, 23 Jun 2014 09:32:13 +0300 Subject: [PATCH 1/3] Avoid direct func pointers to built-ins on MSVC --- src/duk_bi_math.c | 67 +++++++++++++++++++++++++++++++++++++++++++ src/duk_features.h.in | 21 ++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/src/duk_bi_math.c b/src/duk_bi_math.c index 7a5ed4b2..0d7afe31 100644 --- a/src/duk_bi_math.c +++ b/src/duk_bi_math.c @@ -191,8 +191,69 @@ static double duk__pow_fixed(double x, double y) { return DUK_DOUBLE_NAN; } +/* Wrappers for calling standard math library methods. These may be required + * on platforms where one or more of the math built-ins are defined as macros + * or inline functions and are thus not suitable to be used as function pointers. + */ +#if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS) +static double duk__fabs(double x) { + return fabs(x); +} +static double duk__acos(double x) { + return acos(x); +} +static double duk__asin(double x) { + return asin(x); +} +static double duk__atan(double x) { + return atan(x); +} +static double duk__ceil(double x) { + return ceil(x); +} +static double duk__cos(double x) { + return cos(x); +} +static double duk__exp(double x) { + return exp(x); +} +static double duk__floor(double x) { + return floor(x); +} +static double duk__log(double x) { + return log(x); +} +static double duk__sin(double x) { + return sin(x); +} +static double duk__sqrt(double x) { + return sqrt(x); +} +static double duk__tan(double x) { + return tan(x); +} +static double duk__atan2(double x, double y) { + return atan2(x, y); +} +#endif /* DUK_USE_AVOID_PLATFORM_FUNCPTRS */ + /* order must match constants in genbuiltins.py */ static const duk__one_arg_func duk__one_arg_funcs[] = { +#if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS) + duk__fabs, + duk__acos, + duk__asin, + duk__atan, + duk__ceil, + duk__cos, + duk__exp, + duk__floor, + duk__log, + duk__round_fixed, + duk__sin, + duk__sqrt, + duk__tan +#else DUK_FABS, DUK_ACOS, DUK_ASIN, @@ -206,12 +267,18 @@ static const duk__one_arg_func duk__one_arg_funcs[] = { DUK_SIN, DUK_SQRT, DUK_TAN +#endif }; /* order must match constants in genbuiltins.py */ static const duk__two_arg_func duk__two_arg_funcs[] = { +#if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS) + duk__atan2, + duk__pow_fixed +#else DUK_ATAN2, duk__pow_fixed +#endif }; duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx) { diff --git a/src/duk_features.h.in b/src/duk_features.h.in index 688f20b2..2624ff7b 100644 --- a/src/duk_features.h.in +++ b/src/duk_features.h.in @@ -1575,6 +1575,27 @@ typedef FILE duk_file; #define DUK_MEMZERO(p,n) \ DUK_MEMSET((p), 0, (n)) +/* + * Avoiding platform function pointers. + * + * On some platforms built-in functions may be implemented as macros or + * inline functions, so they can't be necessarily addressed by function + * pointers. This is certainly the case with some platform "polyfills" + * which provide missing C99/C++11 functions through macros. + */ + +#undef DUK_USE_AVOID_PLATFORM_FUNCPTRS + +#if !defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS) && defined(DUK_F_MSVC) +/* VS2013 users have sometimes encountered a "error C2099: initializer + * is not a constant" which may be related to some built-in functions + * being macros/inline functions with specific options. For instance, + * with GH-17 the problem disappeared in debug build or with the + * /fp:strict option. So, use wrappers one MSVC. + */ +#define DUK_USE_AVOID_PLATFORM_FUNCPTRS +#endif + /* * Vararg macro wrappers. We need va_copy() which is defined in C99 / C++11, * so an awkward replacement is needed for pre-C99 / pre-C++11 environments. From 48a3dfc97a94f30e1d4c638d902bed6126bcfcce Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Mon, 23 Jun 2014 09:35:26 +0300 Subject: [PATCH 2/3] Avosd func pointers by default on all platforms The cost of this change is minimal (e.g. on x64, ~100 bytes) but it's something that would quite easily crop up on exotic platforms. Fixing it without tinkering with Duktape internals would be difficult for the calling code. --- src/duk_features.h.in | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/duk_features.h.in b/src/duk_features.h.in index 2624ff7b..84ceaaee 100644 --- a/src/duk_features.h.in +++ b/src/duk_features.h.in @@ -1581,20 +1581,12 @@ typedef FILE duk_file; * On some platforms built-in functions may be implemented as macros or * inline functions, so they can't be necessarily addressed by function * pointers. This is certainly the case with some platform "polyfills" - * which provide missing C99/C++11 functions through macros. + * which provide missing C99/C++11 functions through macros, and may be + * the case with VS2013 (see GH-17). */ -#undef DUK_USE_AVOID_PLATFORM_FUNCPTRS - -#if !defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS) && defined(DUK_F_MSVC) -/* VS2013 users have sometimes encountered a "error C2099: initializer - * is not a constant" which may be related to some built-in functions - * being macros/inline functions with specific options. For instance, - * with GH-17 the problem disappeared in debug build or with the - * /fp:strict option. So, use wrappers one MSVC. - */ +/* This is now the default: the cost in footprint is negligible. */ #define DUK_USE_AVOID_PLATFORM_FUNCPTRS -#endif /* * Vararg macro wrappers. We need va_copy() which is defined in C99 / C++11, From df465cddf803af50161b17d69af8894e0ff9b990 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Mon, 23 Jun 2014 09:36:17 +0300 Subject: [PATCH 3/3] Release note update --- RELEASES.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/RELEASES.txt b/RELEASES.txt index 2bf716c8..60aa2758 100644 --- a/RELEASES.txt +++ b/RELEASES.txt @@ -399,6 +399,10 @@ Planned constants internally (e.g. use 0x7fffffffL - 1L instead of -0x80000000L); this fixed a concrete problem with at least VS2010 + x64 +* Portability fix for avoiding direct function pointers to built-in functions + (especially math functions) because they may be implemented as inline + functions or macros on some platforms or polyfill headers + * Portability fixes for MSVC, avoid secure CRT warnings to work better with Windows Store apps