M
Michael Foukarakis
2.A: A new pragma
------------------
#pragma STDC OVERFLOW_CHECK on_off_flag
When in the ON state, any overflow of an addition, subtraction,
multiplication or division provokes a call to the overflow handler.
Operations like +=, -=, *=, and /= are counted also.
Assignment can also cause an overflow. You should concider such cases
as well.
Only the types signed int and signed long long are concerned.
Signed integers that are converted to unsigned types for expression
evaluation are also concerned. Several such cases have led to
exploitable bugs. Pointers that wrap around also cause similar
problems. Do not limit your problem space unless necessary.
The initial state of the overflow flag is implementation defined.
2.B: Setting the handler for overflows
All your function and variable names should be named appropriately to
avoid namespace pollution, but I understand this is an early draft.
Just keep it in mind.
The function set_overflow_handler sets the function to be called in
case of overflow to the specified value. If "newvalue" is NULL,
the function sets the handler to the default value (the value
it had at program startup).
What you describe here implies runtime behaviour. How are you going to
determine "newvalue" in a static context? Are you considering partial
code evaluation? Emulation? Please describe your method more
thoroughly so that appropriate feedback can be given.
2.C: The handler function
-------------------------
typedef void (*overflow_handler_t)(unsigned line_number,
char *filename,
char *function_name,...);
This function will be called when an overflow is detected. The
arguments have the same values as __LINE__ __FILE__ and __FUNC__
If this function returns, execution continues with an implementation
defined value as the result of the operation that overflowed.
I assume this is provided for the user to handle his/her overflows as
deems fit. This is indeed zero-overhead, but is also zero-work. What
exactly is the novelty you're providing us with? There are well known
methods to detect overflows and many have already been posted in clc
lately - what is the fundamental advantage your compiler will provide
me that will give me reason to stop using those and perform my own
error (overflow) handling in my programs?
-------------------------------------------------------------------
Implementation.
I have implemented this solution, and the overhead is almost zero.
If you do not provide concrete numbers you cannot make such claims.
The most important point for implementors is to realize that the
normal flow (i.e. when there is no overflow) should not be disturbed.
No overhead implementation:
--------------------------
1. Perform operation (add, subtract, etc)
2. Jump on overflow to an error label
3: Go on with the rest of the program
The overhead of this is below accuracy in a PC system.
It can't be measured.
Wrong. There are well known implemented methods to measure even number
of instructions executed between two observation points. Use them and
provide us with results for specific test setups.
Implementation with small overhead (3-5%)
1. Perform operation
2. If no overflow jump to continuation
3. save registers
4. Push arguments
5. Call handler
6. Pop arguments
7. Restore registers
continuation:
The problem with the second implementation is that the flow of control
is disturbed.
Your implementation is also changing the flow of control. Let me say
here that implementation-defined does not relieve you of all
responsibility - you could at the very least specify some desired
behaviour or properties that should be held true. You also need to
consider the case where an expression contains more than one variables
| expressions which can overflow. Where does program flow resume then?
You can be pessimistic and return before any of the expressions are
evaluated, but in concurrent environments there is the possibility
something is messed up even then.
The branch to the continuation code will be mispredicted
since it is a forward branch. This provokes pipeline turbulence.
The first solution provokes no pipeline turbulence since the forward
jump will be predicted as not taken.
This is not always true; consider the gcc likely/unlikely macros.
This will be a good prediction
in the overwhelming majority of situations (no overflow). The only
overhead is just an additional instruction, i.e. almost nothing.
Overall, very good initiative, but it needs a lot of work.