Skip to content

Commit

Permalink
Introduce cpu_demo and tty_demo
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdfrench committed Jul 20, 2024
1 parent adaff08 commit d044216
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,21 @@ different algorithms for the same task. The idea behind IFUNC was to allow
programs to check for CPU features the first time a function is called, and
thereafter use an implementation that will be most appropriate for that CPU.

Take a look at [`tty_demo.c`](src/tty_demo.c) for an example. This is a
toy program that prints "Hello World!" when its stdout is a file, but
prints that message using green text if its output is a terminal. It
Take a look at [`cpu_demo.c`](src/cpu_demo.c). This file shows the most
common use of IFUNC: it asks the CPU whether or not it supports certain
features, and provides a different *implementation* of a function
depending on what features are supported. In this case, our function
`print_cpu_info` will end up printing either "AVX2 is present" or
"SSE4.2 is present" depending on how ancient your CPU is.

Unfortunately, IFUNC can be used for other purposes, as Sam James
explains in [FAQ on the xz-utils backdoor (CVE-2024-3094)][thesamesam].
You can see an example of this in [`tty_demo.c`](src/tty_demo.c). This
is a toy program that prints "Hello World!" when its stdout is a file,
but prints that message using green text if its output is a terminal. It
uses IFUNC to load the appropriate implementation when the program
starts.

Unfortunately, IFUNC can be used for other purposes, as Sam James explains in
[FAQ on the xz-utils backdoor (CVE-2024-3094)][thesamesam].


### Isn't that just function pointers?
Yes, it's like function pointers, but slower:
Expand Down Expand Up @@ -203,6 +209,8 @@ There are three things at play here:
* GOT
* RELRO

![](boromir_plt.png)

The PLT and the GOT enable lazy binding. That is what they are *for*. Check out
jasoncc's [GNU Indirect Function and x86 ELF ABIs][jasoncc] for more on this.

Expand Down
4 changes: 2 additions & 2 deletions src/cpu_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void* resolve_cpu_info(void) {
}

int main() {
print_cpu_info(); // Runs resolver first, then cpu-specific function
print_cpu_info(); // Runs the cpu-specific function without the resolver
printf("Entering main\n");
print_cpu_info();
return 0;
}

0 comments on commit d044216

Please sign in to comment.