volatile?

240 просмотров
Перейти к первому непрочитанному сообщению

Oliver Young

не прочитано,
10 авг. 2004 г., 08:12:5110.08.2004

What keyword "volatile" is? What is it doing here:

XSECCryptoKey * XENCCipherImpl::decryptKeyFromKeyInfoList(DSIGKeyInfoList *
kil) {

XSECCryptoKey * ret = NULL;
XSECAlgorithmHandler *handler;

int kLen = (int) kil->getSize();

for (int i = 0; ret == NULL && i < kLen ; ++ i) {

if (kil->item(i)->getKeyInfoType() == DSIGKeyInfo::KEYINFO_ENCRYPTEDKEY) {

XENCEncryptedKey * ek = dynamic_cast<XENCEncryptedKey*>(kil->item(i));
volatile XMLByte buffer[1024];
try {
// Have to cast off volatile
int keySize = decryptKey(ek, (XMLByte *) buffer, 1024);

if (keySize > 0) {
// Try to map the key

XENCEncryptionMethod * encryptionMethod =
mp_encryptedData->getEncryptionMethod();

if (encryptionMethod != NULL) {

handler =
XSECPlatformUtils::g_algorithmMapper->mapURIToHandler(
mp_encryptedData->getEncryptionMethod()->getAlgorithm());

if (handler != NULL)
ret = handler->createKeyForURI(
mp_encryptedData->getEncryptionMethod()->getAlgorithm(),
(XMLByte *) buffer,
keySize);
}
}
} catch (XSECCryptoException &) {
/* Do nothing - this is likely to be a bad decrypt on a public key */
} catch (...) {
memset((void *) buffer, 0, 1024);
throw;
}

// Clear out the key buffer
memset((void *) buffer, 0, 1024);
}
}

return ret;
}


Hendrik Schober

не прочитано,
10 авг. 2004 г., 08:36:3410.08.2004
Oliver Young <none> wrote:
> What keyword "volatile" is?

It advices the compiler to not to perform
certain optimizations which presume that
an object's value wouldn't change behind
the compiler's back. For example, this
code

bool cancel = false;
while( !cancel ) {
;
}

usually could be optimized to

for(;;) {
;
}

(since the compiler sees, that 'cancel' is
never changed). However, if you have this
code

volatile bool cancel = false;
while( !cancel ) {
;
}

instead, the compiler will assume that
'cancel' might change in some magical way
it doesn't know about and won't do the
optimization.

> What is it doing here:

> [...]


I shortly looked at it and I have no idea.

Schobi

--
Spam...@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers


Alex Vinokur

не прочитано,
10 авг. 2004 г., 09:08:5110.08.2004

"Oliver Young" <none> wrote in message news:4118bb98$1...@newsgroups.borland.com...
>
> What keyword "volatile" is?
[snip]

See the thread titled "What is volatile?" in news:comp.lang.c
at http://groups.google.com/groups?threadm=d46b1147.0403162202.74cbd777%40posting.google.com

and sample
at http://groups.google.com/groups?selm=c3904h%2425ua71%241%40ID-79865.news.uni-berlin.de


--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Dennis Jones

не прочитано,
10 авг. 2004 г., 08:44:0710.08.2004

"Oliver Young" <none> wrote in message
news:4118bb98$1...@newsgroups.borland.com...
>
> What keyword "volatile" is? What is it doing here:
>

Volatile tells the compiler not to make any assumptions about the contents
of the variable while evaluating its value in an expression. In other
words, the compiler should always read the value of the variable, rather
than assume its value has not changed since the last time it was read.

Volatile is usually used in embedded systems where variables may be changed
at any time by the hardware. It is also useful in multi-threaded
applications where variables may be changed my more than one thread at a
time.

- Dennis


Harold Howe [TeamB]

не прочитано,
10 авг. 2004 г., 12:21:2010.08.2004
Oliver Young wrote:

> What keyword "volatile" is?

<quote src="ansi/iso C++ std">
volatile is a hint to the implementation to avoid aggressive
optimization involving the object because the value of the object might
be changed by means undetectable by an implementation.
</quote>

The undetectable means are generally interrupts that change global
variables and objects that reside in a memory mapped region. C and C++
programmers typically don't use volatile much when they are targeting
windows (the exception would be people that write drivers and other low
level stuff). Some people suggest making variables that are accessed by
multiple threads volatile, but that is rarely sufficient.

Here is an example that shows how volatile impacts code generation.

#include <cstdio>
using namespace std;

char y = 'a'; // a non-volatile char
volatile char vy='b'; // a volatile char

void test1()
{
vy = y;
++y;
print(y);
}

void test2()
{
y = vy;
++vy;
print(vy);
}

void print(char c)
{
putchar(c);
}


test1 and test2 are symmetric. In test1, a non-volatile value is read
from memory and used in three C++ statements. In test2, a volatile value
is read and used in three successive C++ statements. I compiled this
code with VC++ 7.1, optimization set to /01 (minimize code size). The
compiler generated this code for the two test routines:

void test1()
{
vy = y;
++y;
print(y);
00401065 xor eax,eax
00401067 mov al,byte ptr [y (415B40h)]
0040106C mov byte ptr [vy (415B41h)],al
00401071 inc al
00401073 mov byte ptr [y (415B40h)],al
00401078 push eax
00401079 call print (40100Ah)
0040107E pop ecx
}

void test2()
{
y = vy;
00401086 mov al,byte ptr [vy (415B41h)]
++vy;
0040108B add byte ptr [vy (415B41h)],1
00401092 mov byte ptr [y (415B40h)],al
print(vy);
00401097 xor eax,eax
00401099 mov al,byte ptr [vy (415B41h)]
0040109E push eax
0040109F call print (40100Ah)
004010A4 pop ecx
}

Notice how the two differ. The first thing I noticed is how the
disassembler groups the two. In test1, three C++ statements are grouped
into one chunk of machine code. In test2, there are three blocks of
machine code, one for each C++ statement.

The y variable resides in memory at 0x415B40. In test1, this address is
only accessed twice: once to read and once to write. In between the read
and write, a cached value from the A register is used. The vy variable
resides at 0x415B41. The test2 routine uses this address three times.
Each time the compiler needs vy, it goes back to its memory location and
retrieves it.

This is kind of the gist of what volatile means to the compiler. The
compiler cannot assume that vy's value has remained constant since the
last time it was read. With y it can.

> What is it doing here:

> volatile XMLByte buffer[1024];
> try {
> // Have to cast off volatile
> int keySize = decryptKey(ek, (XMLByte *) buffer, 1024);

A feigned attempt at thread safety, most likely. But I really don't know.

On a different note, notice how they pass the volatile buffer into a
function (decryptKey) that takes a non-volatile argument. Do you think
that is safe? It can't be if the data is truly volatile. decryptKey is
off somewhere else, and it is has probably been compiled with
optimizations turned on (much like test1). Passing it a buffer whose
contents could change without notice can't be good.

<quote src="ansi/iso C++ std">
If an attempt is made to refer to an object defined with a
volatile-qualified type through the use of an lvalue with a
non-qualified type, the program behavior is undefined.
</quote>

I'm not a language lawyer, but isn't that what they are doing by casting
away the volatile and passing it to another function?

H^2

Oliver Young

не прочитано,
11 авг. 2004 г., 03:48:3011.08.2004

Thank you, guys.


Ответить всем
Написать сообщение автору
Переслать
0 новых сообщений