2016-12-30 05:22:11 -06:00
|
|
|
#include "llvm-c/BitReader.h"
|
|
|
|
#include "llvm-c/Core.h"
|
|
|
|
#include "llvm-c/Object.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2020-07-02 13:27:15 -05:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-12-30 05:22:11 -06:00
|
|
|
#include "llvm/Analysis/Lint.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
2013-05-25 17:23:12 -05:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2013-05-27 18:15:31 -05:00
|
|
|
#include "llvm/IR/InlineAsm.h"
|
2016-12-30 05:22:11 -06:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-05-27 18:15:31 -05:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2016-12-30 05:22:11 -06:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2022-11-03 09:34:24 -05:00
|
|
|
#include "llvm/Support/JSON.h"
|
2016-12-30 05:22:11 -06:00
|
|
|
#include "llvm/Support/Host.h"
|
2013-05-27 18:15:31 -05:00
|
|
|
#include "llvm/Support/Memory.h"
|
2016-12-30 05:22:11 -06:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-05-27 18:15:31 -05:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2016-12-30 05:22:11 -06:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2013-05-27 18:15:31 -05:00
|
|
|
#include "llvm/Transforms/Vectorize.h"
|
|
|
|
|
2016-12-30 05:22:11 -06:00
|
|
|
#define LLVM_VERSION_GE(major, minor) \
|
|
|
|
(LLVM_VERSION_MAJOR > (major) || \
|
|
|
|
LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR >= (minor))
|
2016-09-24 09:44:21 -05:00
|
|
|
|
2017-07-21 06:13:37 -05:00
|
|
|
#define LLVM_VERSION_LT(major, minor) (!LLVM_VERSION_GE((major), (minor)))
|
|
|
|
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
|
|
|
|
2016-11-17 08:10:19 -06:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
|
|
|
|
2014-03-31 16:43:19 -05:00
|
|
|
#include "llvm/IR/DIBuilder.h"
|
2016-12-30 05:22:11 -06:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2014-03-31 16:43:19 -05:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2014-02-26 16:06:27 -06:00
|
|
|
|
2023-02-07 08:48:54 -06:00
|
|
|
#if LLVM_VERSION_GE(16, 0)
|
|
|
|
#include "llvm/TargetParser/Triple.h"
|
|
|
|
#else
|
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#endif
|
|
|
|
|
2018-01-04 14:19:23 -06:00
|
|
|
extern "C" void LLVMRustSetLastError(const char *);
|
2014-09-10 01:12:09 -05:00
|
|
|
|
2016-12-30 05:22:11 -06:00
|
|
|
enum class LLVMRustResult { Success, Failure };
|
2016-08-01 16:16:16 -05:00
|
|
|
|
2016-11-16 16:36:08 -06:00
|
|
|
enum LLVMRustAttribute {
|
2016-12-30 05:22:11 -06:00
|
|
|
AlwaysInline = 0,
|
|
|
|
ByVal = 1,
|
|
|
|
Cold = 2,
|
|
|
|
InlineHint = 3,
|
|
|
|
MinSize = 4,
|
|
|
|
Naked = 5,
|
|
|
|
NoAlias = 6,
|
|
|
|
NoCapture = 7,
|
|
|
|
NoInline = 8,
|
|
|
|
NonNull = 9,
|
|
|
|
NoRedZone = 10,
|
|
|
|
NoReturn = 11,
|
|
|
|
NoUnwind = 12,
|
|
|
|
OptimizeForSize = 13,
|
|
|
|
ReadOnly = 14,
|
|
|
|
SExt = 15,
|
|
|
|
StructRet = 16,
|
|
|
|
UWTable = 17,
|
|
|
|
ZExt = 18,
|
|
|
|
InReg = 19,
|
2016-12-29 22:28:11 -06:00
|
|
|
SanitizeThread = 20,
|
|
|
|
SanitizeAddress = 21,
|
|
|
|
SanitizeMemory = 22,
|
2018-09-26 11:19:55 -05:00
|
|
|
NonLazyBind = 23,
|
2018-10-27 07:29:06 -05:00
|
|
|
OptimizeNone = 24,
|
2019-02-09 08:55:30 -06:00
|
|
|
ReturnsTwice = 25,
|
2020-02-17 15:36:01 -06:00
|
|
|
ReadNone = 26,
|
2021-01-22 20:32:38 -06:00
|
|
|
SanitizeHWAddress = 28,
|
2021-02-20 10:02:23 -06:00
|
|
|
WillReturn = 29,
|
add rustc option for using LLVM stack smash protection
LLVM has built-in heuristics for adding stack canaries to functions. These
heuristics can be selected with LLVM function attributes. This patch adds a
rustc option `-Z stack-protector={none,basic,strong,all}` which controls the use
of these attributes. This gives rustc the same stack smash protection support as
clang offers through options `-fno-stack-protector`, `-fstack-protector`,
`-fstack-protector-strong`, and `-fstack-protector-all`. The protection this can
offer is demonstrated in test/ui/abi/stack-protector.rs. This fills a gap in the
current list of rustc exploit
mitigations (https://doc.rust-lang.org/rustc/exploit-mitigations.html),
originally discussed in #15179.
Stack smash protection adds runtime overhead and is therefore still off by
default, but now users have the option to trade performance for security as they
see fit. An example use case is adding Rust code in an existing C/C++ code base
compiled with stack smash protection. Without the ability to add stack smash
protection to the Rust code, the code base artifacts could be exploitable in
ways not possible if the code base remained pure C/C++.
Stack smash protection support is present in LLVM for almost all the current
tier 1/tier 2 targets: see
test/assembly/stack-protector/stack-protector-target-support.rs. The one
exception is nvptx64-nvidia-cuda. This patch follows clang's example, and adds a
warning message printed if stack smash protection is used with this target (see
test/ui/stack-protector/warn-stack-protector-unsupported.rs). Support for tier 3
targets has not been checked.
Since the heuristics are applied at the LLVM level, the heuristics are expected
to add stack smash protection to a fraction of functions comparable to C/C++.
Some experiments demonstrating how Rust code is affected by the different
heuristics can be found in
test/assembly/stack-protector/stack-protector-heuristics-effect.rs. There is
potential for better heuristics using Rust-specific safety information. For
example it might be reasonable to skip stack smash protection in functions which
transitively only use safe Rust code, or which uses only a subset of functions
the user declares safe (such as anything under `std.*`). Such alternative
heuristics could be added at a later point.
LLVM also offers a "safestack" sanitizer as an alternative way to guard against
stack smashing (see #26612). This could possibly also be included as a
stack-protection heuristic. An alternative is to add it as a sanitizer (#39699).
This is what clang does: safestack is exposed with option
`-fsanitize=safe-stack`.
The options are only supported by the LLVM backend, but as with other codegen
options it is visible in the main codegen option help menu. The heuristic names
"basic", "strong", and "all" are hopefully sufficiently generic to be usable in
other backends as well.
Reviewed-by: Nikita Popov <nikic@php.net>
Extra commits during review:
- [address-review] make the stack-protector option unstable
- [address-review] reduce detail level of stack-protector option help text
- [address-review] correct grammar in comment
- [address-review] use compiler flag to avoid merging functions in test
- [address-review] specify min LLVM version in fortanix stack-protector test
Only for Fortanix test, since this target specifically requests the
`--x86-experimental-lvi-inline-asm-hardening` flag.
- [address-review] specify required LLVM components in stack-protector tests
- move stack protector option enum closer to other similar option enums
- rustc_interface/tests: sort debug option list in tracking hash test
- add an explicit `none` stack-protector option
Revert "set LLVM requirements for all stack protector support test revisions"
This reverts commit a49b74f92a4e7d701d6f6cf63d207a8aff2e0f68.
2021-04-06 14:37:49 -05:00
|
|
|
StackProtectReq = 30,
|
|
|
|
StackProtectStrong = 31,
|
|
|
|
StackProtect = 32,
|
2022-02-05 00:00:37 -06:00
|
|
|
NoUndef = 33,
|
2021-12-03 15:11:13 -06:00
|
|
|
SanitizeMemTag = 34,
|
2022-07-06 21:07:52 -05:00
|
|
|
NoCfCheck = 35,
|
2022-06-17 13:14:58 -05:00
|
|
|
ShadowCallStack = 36,
|
2022-03-21 14:30:54 -05:00
|
|
|
AllocSize = 37,
|
2022-07-30 11:53:51 -05:00
|
|
|
#if LLVM_VERSION_GE(15, 0)
|
2022-03-21 14:30:54 -05:00
|
|
|
AllocatedPointer = 38,
|
|
|
|
AllocAlign = 39,
|
2022-07-30 11:53:51 -05:00
|
|
|
#endif
|
2023-05-19 18:30:15 -05:00
|
|
|
SanitizeSafeStack = 40,
|
2016-11-16 16:36:08 -06:00
|
|
|
};
|
|
|
|
|
2014-09-10 01:12:09 -05:00
|
|
|
typedef struct OpaqueRustString *RustStringRef;
|
2014-09-12 10:17:58 -05:00
|
|
|
typedef struct LLVMOpaqueTwine *LLVMTwineRef;
|
2014-09-27 03:33:36 -05:00
|
|
|
typedef struct LLVMOpaqueSMDiagnostic *LLVMSMDiagnosticRef;
|
2014-09-10 01:12:09 -05:00
|
|
|
|
2016-12-31 11:01:23 -06:00
|
|
|
extern "C" void LLVMRustStringWriteImpl(RustStringRef Str, const char *Ptr,
|
|
|
|
size_t Size);
|
2014-09-10 01:12:09 -05:00
|
|
|
|
2016-12-31 11:01:23 -06:00
|
|
|
class RawRustStringOstream : public llvm::raw_ostream {
|
|
|
|
RustStringRef Str;
|
|
|
|
uint64_t Pos;
|
2014-09-10 01:12:09 -05:00
|
|
|
|
2016-12-31 11:01:23 -06:00
|
|
|
void write_impl(const char *Ptr, size_t Size) override {
|
|
|
|
LLVMRustStringWriteImpl(Str, Ptr, Size);
|
|
|
|
Pos += Size;
|
2016-12-30 05:22:11 -06:00
|
|
|
}
|
2014-09-10 01:12:09 -05:00
|
|
|
|
2016-12-31 11:01:23 -06:00
|
|
|
uint64_t current_pos() const override { return Pos; }
|
2014-09-10 01:12:09 -05:00
|
|
|
|
|
|
|
public:
|
2016-12-31 11:01:23 -06:00
|
|
|
explicit RawRustStringOstream(RustStringRef Str) : Str(Str), Pos(0) {}
|
2014-09-10 01:12:09 -05:00
|
|
|
|
2016-12-31 11:01:23 -06:00
|
|
|
~RawRustStringOstream() {
|
2016-12-30 05:22:11 -06:00
|
|
|
// LLVM requires this.
|
|
|
|
flush();
|
|
|
|
}
|
2014-09-10 01:12:09 -05:00
|
|
|
};
|