mailing list of musl libc
 help / color / mirror / code / Atom feed
* Left-shift of negative number
@ 2015-07-17 18:28 Loïc Runarvot
  2015-07-17 19:02 ` Jens Gustedt
  2015-07-17 21:28 ` Rich Felker
  0 siblings, 2 replies; 11+ messages in thread
From: Loïc Runarvot @ 2015-07-17 18:28 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 992 bytes --]


According to the C11 standard, doing a left-shift on a negative integer is considered as an undefined behavior (6.5.7:4).

This undefined behavior occurs in files src/multibyte/internal.c and src/multibyte/internal.h.
At line 21 in the header (http://git.musl-libc.org/cgit/musl/tree/src/multibyte/internal.h?id=0f9c2666aca95eb98eb0ef4f4d8d1473c8ce3fa0#n21),
the implementation of the macro-definition R allow to have a negative value on the expression ((a == 0x80) ? 0x40-b : -a) << 23.

In fact, in the source file, at the line 11 (http://git.musl-libc.org/cgit/musl/tree/src/multibyte/internal.c?id=0f9c2666aca95eb98eb0ef4f4d8d1473c8ce3fa0#n11).
During the application of the macro-definition R(0x90, 0xc0), we have a != 0x90, so it's try to do (-0x90) << 23, which is an undefined behavior.

This bug was found in the context of the libc cross-testing project (a post blog has been written on this subject yesterday: http://trust-in-soft.com/the-libc-cross-testing-project)


[-- Attachment #2: Type: text/html, Size: 1708 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: Left-shift of negative number
@ 2015-07-17 19:55 Pascal Cuoq
  0 siblings, 0 replies; 11+ messages in thread
From: Pascal Cuoq @ 2015-07-17 19:55 UTC (permalink / raw)
  To: musl

Jens Gustedt wrote:

> Am Freitag, den 17.07.2015, 18:28 +0000 schrieb Loïc Runarvot:
> > According to the C11 standard, doing a left-shift on a negative
> > integer is considered as an undefined behavior (6.5.7:4).
> 
> There is no such thing as "considered undefined behavior",

You are right, the word “considered” is unnecessary.
A left shift on a negative integer is undefined behavior, period.

> this is a
> terminology that makes not much sense. Unfortunately the term
> "undefined behavior" is often used as a synonym for "errorneous code"
> which it just isn't.

But in this case, it is used as a synonym for “being undefined behavior”,
which it is. Or are we reading the phrase “otherwise, the behavior is undefined”
in the relevant clause of the standard differently?

I know that this is not well-known, so perhaps I should explain: ever since
the late 1990s, C compilers have been taking advantage of undefined behavior
in the C standard to introduce new optimizations. So if you rely on undefined
behavior in your C programs, they may not work today, and they may break
tomorrow. For instance, a claim very similar to yours:

> Whereas we can
> reasonably assume that a shift of a negative value in two's complement
> is the same as an unsigned shift,

may be used to justify that INT_MAX + 1 results in INT_MIN on two's
complement platforms. This line of reasoning does not work too well nowadays,
as anyone who has ever read the assembly generated by a modern optimizing
compiler for the function int f(int x) { return x + 1 > x; } has verified for
themselves.

So what about shifts of negative values then? Should we worry that they
will be broken in 2017? No, there is no need to wait.

According to this post, ICC takes full advantage today of the fact
that they are undefined behavior:

http://stackoverflow.com/questions/22883790/left-shift-of-negative-values

And according to one slide in this deck, MSVC does too(*):

http://fsl.cs.illinois.edu/images/2/27/2011-09-30-CK-MVD.pdf

> But this doesn't mean
> that a particular implementation of a C compiler or the C library
> (here musl) can't define a behavior for that.

I sincerely do not think that it's not musl's place to define the behavior
of -1 << 1, until it comes bundled with a C compiler, because until
then, the behavior of that expression is entirely without its control
and entirely within the control of unsympathetic C compilers that have
the C standard on their side. Also the phrase
“strives to be correct in the sense of standards-conformance…”
would need to be amended to something like “works with at least
two GCC-like C compilers”.

Pascal

(*) by the way, do not remember anything that these slides say about
Frama-C. The authors of the slides, when talking about it, have no idea
what the f-word they are talking about, and they have a research the usefulness
of which they conveniently want to impress you with.



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2015-07-26 21:23 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-17 18:28 Left-shift of negative number Loïc Runarvot
2015-07-17 19:02 ` Jens Gustedt
2015-07-17 19:38   ` Alex
2015-07-17 21:35   ` Rich Felker
2015-07-18 20:01     ` ibid.ag
2015-07-17 21:28 ` Rich Felker
2015-07-25  3:22   ` Rich Felker
2015-07-25 18:26     ` Alexander Cherepanov
2015-07-26 16:53       ` Rich Felker
2015-07-26 21:23         ` Alexander Cherepanov
2015-07-17 19:55 Pascal Cuoq

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).