Merge pull request #15914 from poettering/ubsan-float-check

json: disable ubsan float checking
This commit is contained in:
Evgeny Vereshchagin 2020-05-26 15:11:16 +03:00 committed by GitHub
commit a53a85b35e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 58 additions and 50 deletions

View File

@ -84,6 +84,14 @@
#define _variable_no_sanitize_address_
#endif
/* Apparently there's no has_feature() call defined to check for ubsan, hence let's define this
* unconditionally on llvm */
#if defined(__clang__)
#define _function_no_sanitize_float_cast_overflow_ __attribute__((no_sanitize("float-cast-overflow")))
#else
#define _function_no_sanitize_float_cast_overflow_
#endif
/* Temporarily disable some warnings */
#define DISABLE_WARNING_FORMAT_NONLITERAL \
_Pragma("GCC diagnostic push"); \
@ -114,6 +122,14 @@
_Pragma("GCC diagnostic push")
#endif
#define DISABLE_WARNING_FLOAT_EQUAL \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wfloat-equal\"")
#define DISABLE_WARNING_TYPE_LIMITS \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wtype-limits\"")
#define REENABLE_WARNING \
_Pragma("GCC diagnostic pop")

View File

@ -707,8 +707,7 @@ static int bus_cgroup_set_boolean(
return 1; \
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
DISABLE_WARNING_TYPE_LIMITS;
BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_weight, CGROUP_MASK_CPU, CGROUP_WEIGHT_IS_OK, CGROUP_WEIGHT_INVALID);
BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares, CGROUP_MASK_CPU, CGROUP_CPU_SHARES_IS_OK, CGROUP_CPU_SHARES_INVALID);
BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight, CGROUP_MASK_IO, CGROUP_WEIGHT_IS_OK, CGROUP_WEIGHT_INVALID);
@ -716,7 +715,7 @@ BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight, CGROUP_MASK_BLKIO, CGROUP_BLKIO_WEI
BUS_DEFINE_SET_CGROUP_LIMIT(memory, CGROUP_MASK_MEMORY, physical_memory_scale, 1);
BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection, CGROUP_MASK_MEMORY, physical_memory_scale, 0);
BUS_DEFINE_SET_CGROUP_LIMIT(swap, CGROUP_MASK_MEMORY, physical_memory_scale, 0);
#pragma GCC diagnostic pop
REENABLE_WARNING;
static int bus_cgroup_set_tasks_max(
Unit *u,

View File

@ -123,12 +123,12 @@ _printf_(2, 3) static int log_callback(int type, const char *fmt, ...) {
fmt2 = strjoina("selinux: ", fmt);
va_start(ap, fmt);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
DISABLE_WARNING_FORMAT_NONLITERAL;
log_internalv(LOG_AUTH | callback_type_to_priority(type),
0, PROJECT_FILE, __LINE__, __FUNCTION__,
fmt2, ap);
#pragma GCC diagnostic pop
REENABLE_WARNING;
va_end(ap);
return 0;

View File

@ -263,10 +263,10 @@ static int write_dependency(
res = strv_join(units, " ");
if (!res)
return log_oom();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
DISABLE_WARNING_FORMAT_NONLITERAL;
fprintf(f, format, res);
#pragma GCC diagnostic pop
REENABLE_WARNING;
}
return 0;

View File

@ -78,10 +78,9 @@ int mhd_respondf(struct MHD_Connection *connection,
errno = -error;
fmt = strjoina(format, "\n");
va_start(ap, format);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
DISABLE_WARNING_FORMAT_NONLITERAL;
r = vasprintf(&m, fmt, ap);
#pragma GCC diagnostic pop
REENABLE_WARNING;
va_end(ap);
if (r < 0)

View File

@ -499,10 +499,9 @@ static void log_xkb(struct xkb_context *ctx, enum xkb_log_level lvl, const char
const char *fmt;
fmt = strjoina("libxkbcommon: ", format);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
DISABLE_WARNING_FORMAT_NONLITERAL;
log_internalv(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, fmt, args);
#pragma GCC diagnostic pop
REENABLE_WARNING;
}
#define LOAD_SYMBOL(symbol, dl, name) \

View File

@ -253,10 +253,9 @@ static JsonVariant *json_variant_formalize(JsonVariant *v) {
return json_variant_unsigned(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_UNSIGNED : v;
case JSON_VARIANT_REAL:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
DISABLE_WARNING_FLOAT_EQUAL;
return json_variant_real(v) == 0.0 ? JSON_VARIANT_MAGIC_ZERO_REAL : v;
#pragma GCC diagnostic pop
REENABLE_WARNING;
case JSON_VARIANT_STRING:
return isempty(json_variant_string(v)) ? JSON_VARIANT_MAGIC_EMPTY_STRING : v;
@ -353,13 +352,12 @@ int json_variant_new_real(JsonVariant **ret, long double d) {
assert_return(ret, -EINVAL);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
DISABLE_WARNING_FLOAT_EQUAL;
if (d == 0.0) {
#pragma GCC diagnostic pop
*ret = JSON_VARIANT_MAGIC_ZERO_REAL;
return 0;
}
REENABLE_WARNING;
r = json_variant_new(&v, JSON_VARIANT_REAL, sizeof(d));
if (r < 0)
@ -896,11 +894,10 @@ intmax_t json_variant_integer(JsonVariant *v) {
converted = (intmax_t) v->value.real;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
DISABLE_WARNING_FLOAT_EQUAL;
if ((long double) converted == v->value.real)
#pragma GCC diagnostic pop
return converted;
REENABLE_WARNING;
log_debug("Real %Lg requested as integer, and cannot be converted losslessly, returning 0.", v->value.real);
return 0;
@ -944,11 +941,10 @@ uintmax_t json_variant_unsigned(JsonVariant *v) {
converted = (uintmax_t) v->value.real;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
DISABLE_WARNING_FLOAT_EQUAL;
if ((long double) converted == v->value.real)
#pragma GCC diagnostic pop
return converted;
REENABLE_WARNING;
log_debug("Real %Lg requested as unsigned integer, and cannot be converted losslessly, returning 0.", v->value.real);
return 0;
@ -1097,9 +1093,12 @@ JsonVariantType json_variant_type(JsonVariant *v) {
return v->type;
}
bool json_variant_has_type(JsonVariant *v, JsonVariantType type) {
_function_no_sanitize_float_cast_overflow_ bool json_variant_has_type(JsonVariant *v, JsonVariantType type) {
JsonVariantType rt;
/* Note: we turn off ubsan float cast overflo detection for this function, since it would complain
* about our float casts but we do them explicitly to detect conversion errors. */
v = json_variant_dereference(v);
if (!v)
return false;
@ -1137,14 +1136,15 @@ bool json_variant_has_type(JsonVariant *v, JsonVariantType type) {
if (rt == JSON_VARIANT_UNSIGNED && type == JSON_VARIANT_REAL)
return (uintmax_t) (long double) v->value.unsig == v->value.unsig;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
DISABLE_WARNING_FLOAT_EQUAL;
/* Any real that can be converted losslessly to an integer and back may also be considered an integer */
if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_INTEGER)
return (long double) (intmax_t) v->value.real == v->value.real;
if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_UNSIGNED)
return (long double) (uintmax_t) v->value.real == v->value.real;
#pragma GCC diagnostic pop
REENABLE_WARNING;
return false;
}
@ -1298,10 +1298,9 @@ bool json_variant_equal(JsonVariant *a, JsonVariant *b) {
return json_variant_unsigned(a) == json_variant_unsigned(b);
case JSON_VARIANT_REAL:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
DISABLE_WARNING_FLOAT_EQUAL;
return json_variant_real(a) == json_variant_real(b);
#pragma GCC diagnostic pop
REENABLE_WARNING;
case JSON_VARIANT_BOOLEAN:
return json_variant_boolean(a) == json_variant_boolean(b);
@ -4095,10 +4094,9 @@ int json_dispatch_uid_gid(const char *name, JsonVariant *variant, JsonDispatchFl
assert_cc(sizeof(uid_t) == sizeof(uint32_t));
assert_cc(sizeof(gid_t) == sizeof(uint32_t));
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
DISABLE_WARNING_TYPE_LIMITS;
assert_cc(((uid_t) -1 < (uid_t) 0) == ((gid_t) -1 < (gid_t) 0));
#pragma GCC diagnostic pop
REENABLE_WARNING;
if (json_variant_is_null(variant)) {
*uid = UID_INVALID;

View File

@ -633,8 +633,7 @@ static void test_tempfn(void) {
static const char chars[] =
"Aąę„”\n\377";
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
DISABLE_WARNING_TYPE_LIMITS;
static void test_fgetc(void) {
_cleanup_fclose_ FILE *f = NULL;
@ -665,7 +664,7 @@ static void test_fgetc(void) {
assert_se(safe_fgetc(f, &c) == 0);
}
#pragma GCC diagnostic pop
REENABLE_WARNING;
static const char buffer[] =
"Some test data\n"

View File

@ -231,10 +231,9 @@ static void test_zeroes(JsonVariant *v) {
assert_se(json_variant_integer(w) == 0);
assert_se(json_variant_unsigned(w) == 0U);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
DISABLE_WARNING_FLOAT_EQUAL;
assert_se(json_variant_real(w) == 0.0L);
#pragma GCC diagnostic pop
REENABLE_WARNING;
assert_se(json_variant_is_integer(w));
assert_se(json_variant_is_unsigned(w));

View File

@ -14,7 +14,7 @@
/* Print information about various types. Useful when diagnosing
* gcc diagnostics on an unfamiliar architecture. */
#pragma GCC diagnostic ignored "-Wtype-limits"
DISABLE_WARNING_TYPE_LIMITS;
#define info(t) \
printf("%s → %zu bits%s, %zu byte alignment\n", STRINGIFY(t), \

View File

@ -363,10 +363,9 @@ static const char * const ntp_leap_table[4] = {
[3] = "not synchronized",
};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
DISABLE_WARNING_TYPE_LIMITS;
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(ntp_leap, uint32_t);
#pragma GCC diagnostic pop
REENABLE_WARNING;
static int print_ntp_status_info(NTPStatusInfo *i) {
char ts[FORMAT_TIMESPAN_MAX], jitter[FORMAT_TIMESPAN_MAX],

View File

@ -449,11 +449,10 @@ static int names_platform(sd_device *dev, struct netnames *names, bool test) {
* The Vendor (3 or 4 char), followed by hexdecimal model number : instance id.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
DISABLE_WARNING_FORMAT_NONLITERAL;
if (sscanf(syspath, pattern, vendor, &model, &instance, &ethid) != 4)
return -EINVAL;
#pragma GCC diagnostic pop
REENABLE_WARNING;
if (!in_charset(vendor, validchars))
return -ENOENT;

View File

@ -0,0 +1 @@
[7E73]