Compiler Explorer include trick

I often use Compiler Explorer to test out new Fortran ideas. Today I’ve just discovered that the C include mechanism can also be used to fetch files at internet locations.

To demonstrate this, I’ve attached to this post a module with the following contents:

module greetings_mod
contains
subroutine greetings()
print *, "Greetings from Fortran Discourse"
end subroutine
end module

greetings_mod.f90.txt (122 Bytes)

When you click on the attachment, you actually get redirected to the address,

https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/free1/uploads/fortran_lang/original/2X/f/fb79e1b65ec5e698fdbd1cd5be536ad878b3f252.txt

(I used the webpage WhereGoes to figure this out)

Now you can include this file by using the C preprocessor,

! In Compiler Explorer
#include "https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/free1/uploads/fortran_lang/original/2X/f/fb79e1b65ec5e698fdbd1cd5be536ad878b3f252.txt"

program main
use greetings_mod
call greetings()
end program

To see the output, visit Compiler Explorer.


Here is a demo of fetching a file from Fortran stdlib:

! In Compiler Explorer
#include "https://raw.githubusercontent.com/fortran-lang/stdlib/refs/heads/master/src/stdlib_system.F90"

program main
use stdlib_system, only: sleep
print *, "Sleeeeeeping..."
call sleep(millisec=2000)
print *, "Woken up."
end program

If you like Compiler Explorer, please consider becoming a sponsor or a contributor. (This is just my personal endorsement.)

Have fun!

7 Likes

Great tip. I was trying earlier to set up the fortran-lang intrinsic descriptions so you could click on the demo program in each description and have it go straight into the playground, which looked like it could work but the playground is rarely working. So I had a Fortran program that could take a Fortran program and convert it to a HTML document that would let you view the code or run it in the playground, as show here

https://urbanjost.github.io/M_intrinsics/playground.html

and then tried getting it to work with other sites, but primarily Compiler Explorer. I was not getting it to work and then found a github project that did something very much like that and have not found it since. This is a LOT simpler. One goal was to see if we could actually get it set up so if you entered code in a Fortran Discourse discussion that you could automatically click on it and it would copy it to Compiler Explorer, but I never got to the point of even figuring out if the hooks existed in Discourse or if they would have to be made.

If you could just click on code in Discourse or on fortran-lang and other sites (even github I/O pages and run the code that would be great. I thought the playground would be ideal, as we could wire it to run with fpm dependencies, stdlib, …

The Fortran include does not limit it to local files, but that is all I have seen implemented. I made a preprocessor that would do a $INCLUDE and call wget, curl, scp, … but removed it from a public version because it introduced a lot of platform dependencies. Do ANY Fortran INCLUDEs allow that?

So, since I never got that going this is a great tip which I think will be very useful to the Fortran community.

The standard allows it, quoting from J3/24-007 (section 6.4),

Additional text can be incorporated into the source text of a program unit during processing. This is accomplished with the INCLUDE line, which has the form
INCLUDE char-literal-constant
[…]
The interpretation of char-literal-constant is processor dependent. An example of a possible valid interpretation
is that char-literal-constant is the name of a file that contains the source text to be included.

I assume that Compiler Explorer is probably using a modified version of cpp.

The WASM backend of LFortran would be perfect for this, because it would run locally on the client side within the browser.

Fortran INCLUDE is distinct from cpp #include, and there are sometimes some unexpected interactions between the two. For example, on some compilers nested #include files work correctly, and nested INCLUDE files work correctly, but if an INCLUDE file has a #include within it, then it will not work. This is because all of the cpp stuff is done before any of the fortran stuff.

Isn’t this a huge security risk?

That was my first thought, to the point that I went hunting for the documentation to see if this was an intentional feature and not a bug in the website front end, because SURELY no one would have intentionally coded this in, right?

Wrong. (FAQ · compiler-explorer/compiler-explorer Wiki · GitHub)

The argument is probably something along the lines of that the website is accepting untrusted user input anyway so this is yet another way to upload untrusted user input and thus doesn’t introduce any new risks, but I would be shocked if there wasn’t an edge case they didn’t think of that an attacker could use to defeat whatever canonicalization/sanitization they’re using.

Reminds me of the C23 #embed thread where people were cheering the ability to import binary blobs. I wonder if, post xz-utils, people are still so eager for that feature.