473,657 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "Environmen tal
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 1606
Spiros Bousbouras <sp****@gmail.c omwrites:
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 "Environmen tal
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.stanfo rd.eduwrote:
Spiros Bousbouras <spi...@gmail.c omwrites:
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 "Environmen tal
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.stanfo rd.eduwrote:
Spiros Bousbouras <spi...@gmail.c omwrites:
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 "Environmen tal
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************ **********@k79g 2000hse.googleg roups.com>,
Spiros Bousbouras <sp****@gmail.c omwrote:
>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 "Environmen tal
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
--
"Considerat ion 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.c omwrites:
[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************ **********@m36g 2000hse.googleg roups.com>
Spiros Bousbouras <sp****@gmail.c omwrote:
>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(labe l1), 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
2283
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
2122
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 setjmp/longjmp, but I am somewhat lost in the detail of how to actually implement this in assembler. (My compiler supports in-line assembler which I hope will prove usefull). I will be very gratefull for any help or pointers with this problem. I am...
18
2438
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 compatibility problems. The contents of the jmp_buf buffer are specific
5
1867
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
20
2310
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 stack; };
8
2410
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
7559
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
2422
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. They (my co-workers) say (with vehement ardor ;) ) that the usage of setjmp() emplyoyed in function"C" that was called from function "B" that was called from function "A" that was called form the main(), will cause havoc in the stack. And it makes...
4
2070
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 ctypes might be patched to offer setjmp support in foreign function definitions? Richard
0
8315
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8608
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.