From: shyouhei@... Date: 2016-04-04T07:20:26+00:00 Subject: [ruby-core:74799] [Ruby trunk Bug#12191] Violation of ANSI aliasing rules causing problems while compiling Issue #12191 has been updated by Shyouhei Urabe. Does this help? RBasic and RBasicRaw does in fact occupy identical memory region so making them union seems the most natural way. ~~~patch diff --git a/internal.h b/internal.h index 3970431..498a7e3 100644 --- a/internal.h +++ b/internal.h @@ -1099,9 +1099,12 @@ NORETURN(void rb_undefined_alloc(VALUE klass)); double rb_num_to_dbl(VALUE val); VALUE rb_obj_dig(int argc, VALUE *argv, VALUE self, VALUE notfound); -struct RBasicRaw { - VALUE flags; - VALUE klass; +union RBasicCast { + struct RBasic opaque; + struct RBasicRaw { + VALUE flags; + VALUE klass; + } transparent; }; #define RBASIC_CLEAR_CLASS(obj) (((struct RBasicRaw *)((VALUE)(obj)))->klass = 0) ~~~ ---------------------------------------- Bug #12191: Violation of ANSI aliasing rules causing problems while compiling https://bugs.ruby-lang.org/issues/12191#change-57922 * Author: Zarko Todorovski * Status: Open * Priority: Normal * Assignee: * ruby -v: * Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN ---------------------------------------- Hi, I work with IBM's XL compiler and we're noticing that there we're getting compile time failures due to ANSI aliasing rule violations. For example, in https://github.com/ruby/ruby/blob/trunk/sprintf.c This function: ~~~C rb_str_vcatf(VALUE str, const char *fmt, va_list ap) { rb_printf_buffer_extra buffer; #define f buffer.base VALUE klass; StringValue(str); rb_str_modify(str); f._flags = __SWR | __SSTR; f._bf._size = 0; f._w = rb_str_capacity(str); f._bf._base = (unsigned char *)str; ** f._p = (unsigned char *)RSTRING_END(str); klass = RBASIC(str)->klass; RBASIC_CLEAR_CLASS(str); ** f.vwrite = ruby__sfvwrite; f.vextra = ruby__sfvextra; buffer.value = 0; BSD_vfprintf(&f, fmt, ap); RBASIC_SET_CLASS_RAW(str, klass); rb_str_resize(str, (char *)f._p - RSTRING_PTR(str)); #undef f return str; } ~~~ When the bolded macros are expanded, they look like this: ~~~ include/ruby/ruby.h:869:#define RSTRING_END(str) \ include/ruby/ruby.h-870- (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \ include/ruby/ruby.h-871- (RSTRING(str)->as.ary + RSTRING_EMBED_LEN(str)) : \ include/ruby/ruby.h-872- (RSTRING(str)->as.heap.ptr + RSTRING(str)->as.heap.len)) include/ruby/ruby.h:1086:#define RSTRING(obj) (R_CAST(RString)(obj)) include/ruby/ruby.h:1082:#define RBASIC(obj) (R_CAST(RBasic)(obj)) include/ruby/ruby.h:1081:#define R_CAST(st) (struct st*) internal.h:852:#define RBASIC_CLEAR_CLASS(obj) (((struct RBasicRaw *)((VALUE)(obj)))->klass = 0) ~~~ The function violates the ANSI aliasing rule since it takes an unsigned long, casts it to a pointer to either RBasic or RBasicRaw and then dereferences it. (RBasic).klass and (RBasicRaw).klass both alias unsigned long, but not each other, as RBasic and RBasicRaw are different types. Additionally, other functions in sprintf.c also seem to have aliasing violations. A fix such as changing line https://github.com/ruby/ruby/blob/trunk/internal.h#L1063 from ~~~ #define RBASIC_CLEAR_CLASS(obj) (((struct RBasicRaw *)((VALUE)(obj)))->klass = 0) ~~~ To: ~~~ #ifdef __ibmxl__ #define RBASIC_CLEAR_CLASS(obj) memset(&(((struct RBasicRaw *)((VALUE)(obj)))->klass), 0, sizeof(((struct RBasicRaw *)((VALUE)(obj)))->klass)) #else #define RBASIC_CLEAR_CLASS(obj) (((struct RBasicRaw *)((VALUE)(obj)))->klass = 0) #endif ~~~ but there should be no need to make a special case for the XL compiler as it's following the ANSI aliasing rules. -- https://bugs.ruby-lang.org/ Unsubscribe: