473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Is this a legal way to use setjmp ?

In the following assume that a is int and
env is of type jmp_buf
a = setjmp(env) ;

Is it legal ? It looks reasonable but it seems to
violate what is mentioned under "Environmental
limits" of 7.13.1.1 of n869 so my guess is that
it's not legal.

If it's not legal could someone give me some insight
on why making this behave correctly would present
difficult problems for an implementation ?

Jun 21 '07 #1
5 1587
Spiros Bousbouras <sp****@gmail.comwrites:
In the following assume that a is int and
env is of type jmp_buf
a = setjmp(env) ;
Is it legal ? It looks reasonable but it seems to
violate what is mentioned under "Environmental
limits" of 7.13.1.1 of n869 so my guess is that
it's not legal.
No.
If it's not legal could someone give me some insight
on why making this behave correctly would present
difficult problems for an implementation ?
The C99 rationale has this to say:

7.13.1.1 The setjmp macro

setjmp is constrained to be a macro only: in some
implementations the information necessary to restore context
is only available while executing the function making the
call to setjmp.

One proposed requirement on setjmp is that it be usable like
any other function, that is, that it be callable in any
expression context, and that the expression evaluate
correctly whether the return from setjmp is direct or via a
call to longjmp. Unfortunately, any implementation of setjmp
as a conventional called function cannot know enough about
the calling environment to save any temporary registers or
dynamic stack locations used part way through an expression
evaluation. (A setjmp macro seems to help only if it expands
to inline assembly code or a call to a special built- in
function.) The temporaries may be correct on the initial
call to setjmp, but are not likely to be on any return
initiated by a corresponding call to longjmp. These
considerations dictated the constraint that setjmp be called
only from within fairly simple expressions, ones not likely
to need temporary storage.

An alternative proposal considered by the C89 Committee was
to require that implementations recognize that calling setjmp
is a special case7, and hence that they take whatever
precautions are necessary to restore the setjmp environment
properly upon a longjmp call. This proposal was rejected on
grounds of consistency: implementations are currently allowed
to implement library functions specially, but no other
situations require special treatment.

--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Jun 21 '07 #2
On 21 Jun, 07:06, Ben Pfaff <b...@cs.stanford.eduwrote:
Spiros Bousbouras <spi...@gmail.comwrites:
In the following assume that a is int and
env is of type jmp_buf
a = setjmp(env) ;
Is it legal ? It looks reasonable but it seems to
violate what is mentioned under "Environmental
limits" of 7.13.1.1 of n869 so my guess is that
it's not legal.

No.
If it's not legal could someone give me some insight
on why making this behave correctly would present
difficult problems for an implementation ?

The C99 rationale has this to say:
<snip>

I had read the relevant part of the rationale before posting
my question. What I'm looking for is a more concrete
explanation regarding the specific example I posted.
Jun 21 '07 #3
On 21 Jun, 07:06, Ben Pfaff <b...@cs.stanford.eduwrote:
Spiros Bousbouras <spi...@gmail.comwrites:
In the following assume that a is int and
env is of type jmp_buf
a = setjmp(env) ;
Is it legal ? It looks reasonable but it seems to
violate what is mentioned under "Environmental
limits" of 7.13.1.1 of n869 so my guess is that
it's not legal.

No.
If it's not legal could someone give me some insight
on why making this behave correctly would present
difficult problems for an implementation ?

The C99 rationale has this to say:

7.13.1.1 The setjmp macro
< partial snip>
An alternative proposal considered by the C89 Committee was
to require that implementations recognize that calling setjmp
is a special case7, and hence that they take whatever
precautions are necessary to restore the setjmp environment
properly upon a longjmp call. This proposal was rejected on
grounds of consistency: implementations are currently allowed
to implement library functions specially, but no other
situations require special treatment.
It occurs to me that not allowing something as basic as
a = setjmp(env) ;
is not very consistent either. Are there other examples of functions
or macros where you cannot do that ?

Jun 21 '07 #4
In article <11**********************@k79g2000hse.googlegroups .com>,
Spiros Bousbouras <sp****@gmail.comwrote:
>In the following assume that a is int and
env is of type jmp_buf
a = setjmp(env) ;

Is it legal ? It looks reasonable but it seems to
violate what is mentioned under "Environmental
limits" of 7.13.1.1 of n869 so my guess is that
it's not legal.

If it's not legal could someone give me some insight
on why making this behave correctly would present
difficult problems for an implementation ?
When longjmp() is called control will return to the calling function
in the middle of the expression a=setjmp(env). In general, that
expression might use arbitrary temporary registers, but if
setjmp() is an ordinary function it will not be able to determine
which registers those are.

I don't find this entirely convincing - setjmp() could just save all
the registers used as temporaries, and if that's too expensive then
you don't *have* to implement it as an ordinary function, and in any
case how many unknown temporaries are going to be used in a simple
assignment? - but then I've never written a C compiler.

Perhaps there were existing implementations that couldn't handle it.
No-one designing a new language would expect to be able to implement
something like setjmp() as a plain function.

-- Richartd
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 21 '07 #5
>>Spiros Bousbouras <spi...@gmail.comwrites:
[given appropriate definitions of a and env]
>>>a = setjmp(env); ... seems to violate [a non-"constraint" "shall"]
limits" of 7.13.1.1 of n869 so my guess is that it's not legal.
Indeed, code of this form uses undefined (at least undefined-
by-the-C-standards) behavior.

In article <11**********************@m36g2000hse.googlegroups .com>
Spiros Bousbouras <sp****@gmail.comwrote:
>I had read the relevant part of the rationale before posting
my question. What I'm looking for is a more concrete
explanation regarding the specific example I posted.
It is somewhat (or even "very" :-) ) difficult to come up with
a good example of how things go wrong here. What it boils down
to, in some sense, is a reluctance to force implementations to
recognize that "setjmp" is not a normal function.

On most machines, control flow tends to involve pushes and
pops onto a stack. This stack may or may not be shared with
that for arguments and/or function return values. When
evaluating complex expressions, this same stack may also be
used by the compiler for compiler-temporaries.

If the compiler believes that setjmp() is an ordinary function,
and therefore it has no "strange stack effects", the compiler
can interleave its own stack manipulations between those that
might occur for any ordinary function call. But if that same
stack is used for control flow, and setjmp() is "returned to"
by a later longjmp() so that control flow is abnormal, there
may be some sort of destructive interference between the two.

The same problem occurs with the (non-Standard) alloca()
function on machines like the x86. Since actual alloca()
implementations alter the stack, calls to alloca() must not
occur "between" operations that also alter the stack. Thus:

char *p;
size_t n;
...
p = alloca(n);

tends to work, but more complex operations like:

foo(p1 = alloca(n), bar(42), p2 = alloca(n), baz(p3 = alloca(n)));

tend to behave badly, because the compiler's stack adjustments
alter alloca()'s stack adjustments, so that p1, p2, and/or p3 may
point to the "wrong part" of the stack.

Simple assignments are likely to work even with "dumb" compilers
that implement setjmp() in the obvious, simple, way (without any
special-casing inside the compiler). More complex operations
like:

foo(setjmp(label1), 42, setjmp(label2));

are clearly asking for trouble. The fact is that setjmp() is like
a goto-label, and longjmp() actually goes to a setjmp() label, and
if a compiler recognizes these through special syntax -- in the
same way that any C99 compiler must now recognize the creation of
a variable-length array due to its special syntax[%] -- it can
arrange for the "right thing" to happen. A future Standard could
expand the list of "allowable setjmp situations" without really
harming anything, except perhaps compiler-writers' free time. :-)

[% Admittedly, VLA syntax looks like any other array definition.
The compiler can "see" that it is a VLA, though, because the
expression(s) that create the dimensions are not compile-time
constant-expressions. The Standard -- either one -- could have
done effectively the same thing with setjmp and longjmp by declaring
that they are keywords, or at least macros that expand to keywords,
even though they *look* like ordinary function calls. The
standards-folk simply chose not to do that.]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note that FILE *fis a local variable in func2.
4
by: Jrferguson | last post by:
I have a C program that I am trying to port to a Motorola 68k based system. It makes use of setjmp and longjmp which are not supported by my C compiler. I understand the general principle behind...
18
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in...
5
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
20
by: JS | last post by:
When setjmp is called how can the if statement evaluate to true or false when setjmp only returns 0 or non-zero? struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int...
8
by: Zheng Da | last post by:
I wrote a simple one as follow: typedef struct __myjmp_buf { int efp; int epc; }myjmp_buf; int mysetjmp(myjmp_buf env) {
4
by: Sreekanth | last post by:
Hi All I am trying to write a code in which I have to access an array of jmp_buf so i have declared it as jmp_buf mybuf Now when i am doing a longjmp like
15
by: rover8898 | last post by:
Hello all, I used setjmp() in a recent of program of mine (it is not completed, so I have not the chance to test it out yet). I am not very profocient in C coding as are some of my co-workers....
4
by: Richard Jones | last post by:
Currently ctypes can't play well with any C code that requires use of setjmp as part of its API. libpng is one of those libraries. Can anyone think of a reasonable solution to this? Perhaps...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.