Created
January 26, 2025 12:12
-
-
Save elupus/42defcd54ab91eb9379c6616ba606725 to your computer and use it in GitHub Desktop.
GLIBC bug with float and swapcontext
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* build with gcc -g -Os -m32 testfp.c */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <ucontext.h> | |
#include <signal.h> | |
#include <assert.h> | |
ucontext_t context_step0; | |
ucontext_t context_step1; | |
ucontext_t context_step2; | |
void step1(void) | |
{ | |
volatile register float x1 = 0.123; | |
volatile register float x2 = 0.234; | |
register float y1; | |
register float y2; | |
y1 = x1 * x2; | |
swapcontext (&context_step1, &context_step2); | |
y2 = x1 * x2; | |
assert (y1 == y2); | |
swapcontext (&context_step1, &context_step2); | |
} | |
void step2(void) | |
{ | |
volatile register float x1 = 0.425; | |
volatile register float x2 = 0.455; | |
register float y1; | |
register float y2; | |
y1 = x1 * x2; | |
swapcontext (&context_step2, &context_step1); | |
y2 = x1 * x2; | |
assert (y1 == y2); | |
swapcontext (&context_step2, &context_step0); | |
} | |
int main(void) | |
{ | |
char step1_stack[SIGSTKSZ]; | |
char step2_stack[SIGSTKSZ]; | |
getcontext(&context_step1); | |
context_step1.uc_link = &context_step0; | |
context_step1.uc_stack.ss_sp = step1_stack; | |
context_step1.uc_stack.ss_size = sizeof(step1_stack); | |
makecontext(&context_step1, (void (*)(void)) step1, 0); | |
getcontext(&context_step2); | |
context_step2.uc_link = &context_step0; | |
context_step2.uc_stack.ss_sp = step2_stack; | |
context_step2.uc_stack.ss_size = sizeof(step2_stack); | |
makecontext(&context_step2, (void (*)(void)) step2, 0); | |
/* start step 1*/ | |
swapcontext (&context_step0, &context_step1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment