mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-20 03:55:48 -04:00

Summary: Quote from http://eel.is/c++draft/expr.add#4: ``` 4 When an expression J that has integral type is added to or subtracted from an expression P of pointer type, the result has the type of P. (4.1) If P evaluates to a null pointer value and J evaluates to 0, the result is a null pointer value. (4.2) Otherwise, if P points to an array element i of an array object x with n elements ([dcl.array]), the expressions P + J and J + P (where J has the value j) point to the (possibly-hypothetical) array element i+j of x if 0≤i+j≤n and the expression P - J points to the (possibly-hypothetical) array element i−j of x if 0≤i−j≤n. (4.3) Otherwise, the behavior is undefined. ``` Therefore, as per the standard, applying non-zero offset to `nullptr` (or making non-`nullptr` a `nullptr`, by subtracting pointer's integral value from the pointer itself) is undefined behavior. (*if* `nullptr` is not defined, i.e. e.g. `-fno-delete-null-pointer-checks` was *not* specified.) To make things more fun, in C (6.5.6p8), applying *any* offset to null pointer is undefined, although Clang front-end pessimizes the code by not lowering that info, so this UB is "harmless". Since rL369789 (D66608 `[InstCombine] icmp eq/ne (gep inbounds P, Idx..), null -> icmp eq/ne P, null`) LLVM middle-end uses those guarantees for transformations. If the source contains such UB's, said code may now be miscompiled. Such miscompilations were already observed: * https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190826/687838.html * https://github.com/google/filament/pull/1566 Surprisingly, UBSan does not catch those issues ... until now. This diff teaches UBSan about these UB's. `getelementpointer inbounds` is a pretty frequent instruction, so this does have a measurable impact on performance; I've addressed most of the obvious missing folds (and thus decreased the performance impact by ~5%), and then re-performed some performance measurements using my [[ https://github.com/darktable-org/rawspeed | RawSpeed ]] benchmark: (all measurements done with LLVM ToT, the sanitizer never fired.) * no sanitization vs. existing check: average `+21.62%` slowdown * existing check vs. check after this patch: average `22.04%` slowdown * no sanitization vs. this patch: average `48.42%` slowdown Reviewers: vsk, filcab, rsmith, aaron.ballman, vitalybuka, rjmccall, #sanitizers Reviewed By: rsmith Subscribers: kristof.beyls, nickdesaulniers, nikic, ychen, dtzWill, xbolva00, dberris, arphaman, rupprecht, reames, regehr, llvm-commits, cfe-commits Tags: #clang, #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D67122 llvm-svn: 374293
124 lines
4.3 KiB
C
124 lines
4.3 KiB
C
// RUN: %clang_cc1 -x c -triple x86_64-apple-darwin10 -w -emit-llvm -o - %s -fsanitize=pointer-overflow | FileCheck %s --check-prefixes=CHECK,CHECK-C
|
|
// RUN: %clang_cc1 -x c++ -triple x86_64-apple-darwin10 -w -emit-llvm -o - %s -fsanitize=pointer-overflow | FileCheck %s --check-prefixes=CHECK,CHECK-CPP
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// CHECK-LABEL: define void @fixed_len_array
|
|
void fixed_len_array(int k) {
|
|
// CHECK: getelementptr inbounds [10 x [10 x i32]], [10 x [10 x i32]]* [[ARR:%.*]], i64 0, i64 [[IDXPROM:%.*]]
|
|
// CHECK-NEXT: [[SMUL:%.*]] = call { i64, i1 } @llvm.smul.with.overflow.i64(i64 40, i64 [[IDXPROM]]), !nosanitize
|
|
// CHECK-NEXT: [[SMULOFLOW:%.*]] = extractvalue { i64, i1 } [[SMUL]], 1, !nosanitize
|
|
// CHECK-NEXT: [[SMULVAL:%.*]] = extractvalue { i64, i1 } [[SMUL]], 0, !nosanitize
|
|
// CHECK-NEXT: [[BASE:%.*]] = ptrtoint [10 x [10 x i32]]* [[ARR]] to i64, !nosanitize
|
|
// CHECK-NEXT: [[COMPGEP:%.*]] = add i64 [[BASE]], [[SMULVAL]], !nosanitize
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}, i64 [[BASE]], i64 [[COMPGEP]]){{.*}}, !nosanitize
|
|
|
|
// CHECK: getelementptr inbounds [10 x i32], [10 x i32]* {{.*}}, i64 0, i64 [[IDXPROM1:%.*]]
|
|
// CHECK-NEXT: @llvm.smul.with.overflow.i64(i64 4, i64 [[IDXPROM1]]), !nosanitize
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
|
|
int arr[10][10];
|
|
arr[k][k];
|
|
}
|
|
|
|
// CHECK-LABEL: define void @variable_len_array
|
|
void variable_len_array(int n, int k) {
|
|
// CHECK: getelementptr inbounds i32, i32* {{.*}}, i64 [[IDXPROM:%.*]]
|
|
// CHECK-NEXT: @llvm.smul.with.overflow.i64(i64 4, i64 [[IDXPROM]]), !nosanitize
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
|
|
// CHECK: getelementptr inbounds i32, i32* {{.*}}, i64 [[IDXPROM1:%.*]]
|
|
// CHECK-NEXT: @llvm.smul.with.overflow.i64(i64 4, i64 [[IDXPROM1]]), !nosanitize
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
|
|
int arr[n][n];
|
|
arr[k][k];
|
|
}
|
|
|
|
// CHECK-LABEL: define void @pointer_array
|
|
void pointer_array(int **arr, int k) {
|
|
// CHECK: @llvm.smul.with.overflow.i64(i64 8, i64 {{.*}}), !nosanitize
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
|
|
// CHECK: @llvm.smul.with.overflow.i64(i64 4, i64 {{.*}}), !nosanitize
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
|
|
arr[k][k];
|
|
}
|
|
|
|
// CHECK-LABEL: define void @pointer_array_unsigned_indices
|
|
void pointer_array_unsigned_indices(int **arr, unsigned k) {
|
|
// CHECK: icmp uge
|
|
// CHECK-NOT: select
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
// CHECK: icmp uge
|
|
// CHECK-NOT: select
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
arr[k][k];
|
|
}
|
|
|
|
// CHECK-LABEL: define void @pointer_array_mixed_indices
|
|
void pointer_array_mixed_indices(int **arr, int i, unsigned j) {
|
|
// CHECK: select
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
// CHECK-NOT: select
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
arr[i][j];
|
|
}
|
|
|
|
struct S1 {
|
|
int pad1;
|
|
union {
|
|
char leaf;
|
|
struct S1 *link;
|
|
} u;
|
|
struct S1 *arr;
|
|
};
|
|
|
|
// TODO: Currently, structure GEPs are not checked, so there are several
|
|
// potentially unsafe GEPs here which we don't instrument.
|
|
//
|
|
// CHECK-LABEL: define void @struct_index
|
|
void struct_index(struct S1 *p) {
|
|
// CHECK: getelementptr inbounds %struct.S1, %struct.S1* [[P:%.*]], i64 10
|
|
// CHECK-NEXT: [[BASE:%.*]] = ptrtoint %struct.S1* [[P]] to i64, !nosanitize
|
|
// CHECK-NEXT: [[COMPGEP:%.*]] = add i64 [[BASE]], 240, !nosanitize
|
|
// CHECK: select
|
|
// CHECK: @__ubsan_handle_pointer_overflow{{.*}} i64 [[BASE]], i64 [[COMPGEP]]) {{.*}}, !nosanitize
|
|
|
|
// CHECK-NOT: @__ubsan_handle_pointer_overflow
|
|
|
|
p->arr[10].u.link->u.leaf;
|
|
}
|
|
|
|
typedef void (*funcptr_t)(void);
|
|
|
|
// CHECK-LABEL: define void @function_pointer_arith
|
|
void function_pointer_arith(funcptr_t *p, int k) {
|
|
// CHECK: add i64 {{.*}}, 8, !nosanitize
|
|
// CHECK-NOT: select
|
|
// CHECK: @__ubsan_handle_pointer_overflow{{.*}}
|
|
++p;
|
|
|
|
// CHECK: @llvm.smul.with.overflow.i64(i64 8, i64 {{.*}}), !nosanitize
|
|
// CHECK: select
|
|
// CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
|
|
p + k;
|
|
}
|
|
|
|
// CHECK-LABEL: define void @dont_emit_checks_for_no_op_GEPs
|
|
// CHECK-C: __ubsan_handle_pointer_overflow
|
|
// CHECK-CPP-NOT: __ubsan_handle_pointer_overflow
|
|
void dont_emit_checks_for_no_op_GEPs(char *p) {
|
|
&p[0];
|
|
|
|
int arr[10][10];
|
|
&arr[0][0];
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|