For Partial Evaluation, you must generate a "residual" version of the original program by specialization where all static data is already bound and evaluated.
Simplest example would be compiling in configuration parameters, so a specific code-path is preselected before runtime. Another classic example would be C++ template meta-programming where that part of the program is not evaluated at runtime.
I first came to know of these techniques from the classic Generative Programming: Methods, Tools, and Applications by Krzysztof Czarnecki and Ulrich W. Eisenecker which is still relevant today.
Checking with Google, it seems GCC does not have a separate partial evaluator but one can use coding techniques+aggressive optimization switches to approximate it. For separate "Futamura Projection" like partial evaluators there seem to be C-Mix/LLPE etc.
> The Tempo specializer has been made publicly available under a licence in April 1998. At of March 99, it was being used by about thirty people around the world. The binary version of Tempo has just (July 2000) been made available without any restrictions, and we expect a full unrestricted source release under GPL to follow soon.
I know this has nothing to do with TFA, just want to check my understanding of C. Doesn't the code example have a bug?
miniprintf(char fmt[], int val[])
{
int i = 0;
while( *fmt != '\0' ) {
if( *fmt != '%' )
putchar(*fmt);
else
switch(*++fmt) {
case 'd' : putint(val[i++]); break;
case '%' : putchar('%'); break;
default : prterror(*fmt); break;
}
fmt++;
}
}
If the static string was "%", the else branch would move past the end of the string. Assuming prterror doesn't crash the program, this reads out-of-bounds, right?
++fmt increments the counter before it's read, so it'll point at the null terminator and enter the default case.
Edit: I guess it may not technically be a bug as it's just a small function, but using a single % instead of %% (which is what printf expects) would be a format error, hence prterror()
Yup, it's a bug. The '%' character gets you into the while loop and into the else clause. The switch increments to the nul terminator, which then executes prterror(0). Then fmt is incremented again so it points just outside of the string. As long as that out-of-bounds byte is not also the nul value the loop continues into out-of-bounds territory.
Wait - does prterror cause the program to exit? It's not defined, so maybe the program does terminate, maybe it doesn't.
Right. If fmt was "%d%d" and val had only one element that'd cause an out-of-bounds read as well. Can't say it's a bug without seeing the rest of the code though; if the function is used correctly everywhere, there's no bug.
RossBencina | 3 hours ago
dmitrygr | 2 hours ago
rramadass | an hour ago
pjmlp | 51 minutes ago
Even though it is its own project, most folks use it alongside GCC.
https://sourceware.org/gdb/current/onlinedocs/gdb.html/Calli...
rramadass | 3 minutes ago
For Partial Evaluation, you must generate a "residual" version of the original program by specialization where all static data is already bound and evaluated.
Simplest example would be compiling in configuration parameters, so a specific code-path is preselected before runtime. Another classic example would be C++ template meta-programming where that part of the program is not evaluated at runtime.
I first came to know of these techniques from the classic Generative Programming: Methods, Tools, and Applications by Krzysztof Czarnecki and Ulrich W. Eisenecker which is still relevant today.
Checking with Google, it seems GCC does not have a separate partial evaluator but one can use coding techniques+aggressive optimization switches to approximate it. For separate "Futamura Projection" like partial evaluators there seem to be C-Mix/LLPE etc.
MiroslavPokorny | an hour ago
rramadass | an hour ago
Partial Evaluation and Automatic Program Generation by Neil Jones et al. - https://web.archive.org/web/20071012081310/http://www.itu.dk...
looperhacks | 40 minutes ago
carlosneves | 35 minutes ago
In your example, "%" would cause switch ('\0') which would fall in the default case.
ginko | 21 minutes ago
The function should probably return on error.
ChrisRR | 23 minutes ago
Edit: I guess it may not technically be a bug as it's just a small function, but using a single % instead of %% (which is what printf expects) would be a format error, hence prterror()
dcuthbertson | 19 minutes ago
Wait - does prterror cause the program to exit? It's not defined, so maybe the program does terminate, maybe it doesn't.
oguz-ismail2 | 19 minutes ago