I'm developing an online judge system for programming contests like LeetCode, Codeforces, etc. As for most programming contests, inline assembler is not allowed in C/C++, so I would like to add the same restriction to my system.
I would like to let GCC and G++ produce an error when compiling a C/C++ program containing inline assembler, so that any code containing inline assembler will result in compilation error.
Is there a way to achieve that? Should I pass some command line arguments to GCC/G++?
Note: disabling inline assembler is just for obeying the rules, not for security concerns.
Is there a way to disable inline assembler in GCC?
Yes there are a couple of methods.
Turn off assembly in the compiler
To do it in compilation phase, use the parameter
-fno-asm
. However, keep in mind that this will only affectasm
and not__asm__
.Documentation:
Define a macro
You can use the parameters
-Dasm=error -D__asm__=error
Note that this construction is generic. What it does is to create macros. It works pretty much like a
#define
. The documentation says:So what it does is simply to change occurrences of
asm
or__asm__
toerror
. This is done in the preprocessor phase. You don't have to useerror
. Just pick anything that will not compile.Use a macro that fires during compilation
A way to solve it in compilation phase by using a macro, as suggested in comments by zwol, you can use
-D'asm(...)=_Static_assert(0,"inline assembly not allowed")'
. This will also solve the problem if there exist an identifier callederror
.Note: This method requires
-std=c11
or higher.Using grep before using gcc
Yet another way that may be the solution to your problem is to just do a
grep
in the root of the source tree before compiling:This will also catch
__asm__
but it may give false positives, for instance is you have a string literal, identifier or comment containing the substring"asm"
. But in your case you could solve this problem by also forbidding any occurrence of that string anywhere in the source code. Just change the rules.Possible unexpected problems
Note that disabling assembly can cause other problems. For instance, I could not use
stdio.h
with this option. It is common that system headers contains inline assembly code.A way to cheat above methods
It is possible to execute strings as machine code. See this answer for an example: https://stackoverflow.com/a/18477070/6699433
A piece of the code from the link above:
The code above is only intended to give a quick idea of how to trick the rules OP has stated. It is not intended to be a good example of how to actually perform it in reality. Furthermore, the code is not mine. It is just a short code quote from the link I supplied. If you have ideas about how to improve it, then please comment on 4pie0:s original post instead.