dangerous C

Аватара пользователя
Сообщения: 2026
Зарегистрирован: 12 апр 2008, 02:53
Откуда: Бразилия
Контактная информация:

dangerous C

#1 Сообщение Хи-Хи » 04 авг 2010, 04:18

FYI. Thought you might be interested.
Сегодня пришло мейлом. Может кому будет интересно и даже полезно.

Sent: Tuesday, August 03, 2010 5:20 PM
To: all-fac
Subject: please share with any C++ coders in your group

I attach a short note on the bad effects of an innocuous-looking way of initializing C++ variables which has cost our collaboration considerable pain, after lying silently in wait for us for many years. You really should listen to the textbook advice on how to initialize variables. The pain is greater, the larger the code base you are dealing with.

A dangerous C++ coding practice to avoid:
float x;
// anything at all not involving x
x = 0.;
Instead always initialize at creation:
float x=0.;
This problem was tracked down by my D0 colleagues, and summarized by Michael Diesburg of Fermilab. Our
reconstruction jobs run from a self‐contained tarball which carries the job’s entire environment (i.e., typical of the grid,
“an endless source of amusing and novel failure modes”). MC reconstruction jobs had run successfully on a particular
farm before. We then tried to reconstruct data on the same farm, but 60% of the jobs failed with floating point
exceptions on the first event. We could not reproduce this behavior elsewhere, even on another Scientific Linux
Fermilab 5 farm. In fact, when we submitted a reconstruction job repeatedly on a single file, it failed 60% of the time
but ran 40% of the time: it wasn’t the data that was bad.
Cause: It usually takes two screw‐ups to really confuse a physicist: your troubles “combine and mix”.
1) Linux kernel had been upgraded to 2.6.12, which includes ASLR (Address Space Layout Randomization), a
security enhancement to thwart hackers using unprotected array overflow vulnerabilities. Your code starts at a
different location in memory each time you run. Thus the offset between your web browser and (say) the
authentication code in the operating system is different each time.
2) In the first float initialization construction above, the compiler has the right to do a register pre‐fetch of x; this is
not a compiler bug (verified with members of C++ standard s committee). If the fetch of an uninitialized variable
finds an illegal floating point number (NaN), a floating point exception will be thrown.
Obviously, these two factors conspire to give stochastic, but disastrous, behavior.
Turn off floating point exceptions: not practical for many physics codes—do you always check before dividing by Cos?
Change floating point units: an alternative exists which doesn’t trip on register loads of NaN
but it also has precision issues; why trade one insidious problem for another
Turn off ASLR: a bandaid, insecure, and only reduces probability, rather than fixes the problem
site security will likely force you to abandon this eventually
Think: have you ever had a job that only ran on the second or third try?
Insanity is doing the same thing but expecting different results—unless you are dealing with computers.
Find instances and fix your code: painful, particularly in large codes, but the only real cure
There may be compiler flags that help; or grep your heart out.
You won’t introduce any new bugs while patching, will you?
Здрасьте, я ваш дядя из далёкой Бразилии, где в лесах много диких мулаток в белых бикини!

Сообщения: 86
Зарегистрирован: 11 апр 2008, 10:48

Re: dangerous C

#2 Сообщение Ersh » 04 авг 2010, 05:54

Бред полнейший. Мало того, что скорее всего кто-то по памяти проехался, так еще вместо того, чтобы искать ошибку, занимаются какой-то фигней, распространяют свои бредовые мысли и что самое удивительное думают что они правы. Вообщем твердых 2 балла таким программистам.

Аватара пользователя
Сообщения: 2026
Зарегистрирован: 12 апр 2008, 02:53
Откуда: Бразилия
Контактная информация:

Re: dangerous C

#3 Сообщение Хи-Хи » 04 авг 2010, 17:55

Когда прочитал ПОВЕРХНОСТНО в 1-й раз, так тоже был удивлён.
Даже скажу больше, что в шоке был :shock:

Всё-таки сейчас уже воздерживаюсь от каких-то категоричных утверждений.
К тому же там всё-таки не один человек с "ума сошёл", если что... а несколько людей в разных солидных местах.
Здрасьте, я ваш дядя из далёкой Бразилии, где в лесах много диких мулаток в белых бикини!


Вернуться в «Программирование»