└── README.md /README.md: -------------------------------------------------------------------------------- 1 | ct_choose: Constant-time choose between two variables 2 | ======================================================= 3 | This project contains Clang/LLVM patches/passes for a new built-in function: 4 | ```c 5 | Type val = __builtin_ct_choose(bool condition, Type valTrue, Type valFalse); 6 | ``` 7 | 8 | This function returns `valTrue` if `condition` is true, else `valFalse`, and does so in constant time. 9 | The X86_64 backend will compile this function into a `CMOV` after other optimizations have run: this should ensure that 10 | the transformation is *not* altered. 11 | 12 | For other backends, there is a default 13 | implementation as `return (trueVal & ~condition) | (falseVal & condition)` with `condition={0,1}`. 14 | Unlike the x86_64, this transformation happens after other IR passes have run, but before the backend passes have: this means IR 15 | passes will not alter the transformation, but there is a risk the backend optimizations will. 16 | 17 | For more information, refer to the paper "[What you get is what you C: Controlling side effects in mainstream C compilers](https://drive.google.com/file/d/1jsOolD1C_Fu9oNVvhkB1_RQ9GlrFSGcN/view)". 18 | 19 | The setup below was tested on Ubuntu trusty 14.04.5 LTS x86_64. I suggest you install this as a VM before reading further. 20 | 21 | Pre-requesites: 22 | --------------- 23 | 1. Install a VM running Ubuntu trusty 14.04.5 LTS x86_64. Allocate 32GB of disk. 24 | 2. $sudo apt-get install git cmake g++ binutils-dev 25 | 26 | Download, compile and install Clang/LLVM: 27 | ----------------------------------------- 28 | $git clone https://github.com/lmrs2/llvm.git -b ctchoose_38 --single-branch --depth 1 29 | $cd llvm/tools 30 | $git clone https://github.com/lmrs2/clang.git -b ctchoose_38 --single-branch --depth 1 31 | $cd .. 32 | $mkdir build 33 | $cd build 34 | $cmake -DLLVM_BINUTILS_INCDIR=/usr/include -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD="X86" ../ 35 | $cmake --build . 36 | $sudo make install 37 | 38 | Now Clang has the new built-in function `__builtin_ct_choose()` which you can call. If you're on x86_64, it should be compiled into a `CMOV`. 39 | --------------------------------------------------------------------------------