Skip to content

[BoundsSafety] Partially Upstream support for BS_CHK_LibCAttributes for Apple platforms #10544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,26 @@ void DarwinClang::addClangTargetOptions(
Action::OffloadKind DeviceOffloadKind) const {

Darwin::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind);

/* TO_UPSTREAM(BoundsSafety) ON*/
// When `-fbounds-safety` and new userspace Libc attributes are enabled
// set a macro that userspace Libc will look for to decide whether or not to
// enable -fbounds-safety annotations in its headers (rdar://84733153). This
// is only being set during the gradual enablement of new checks and will be
// removed once the transition is complete (rdar://137912561).
if (DriverArgs.hasFlagNoClaim(options::OPT_fbounds_safety,
options::OPT_fno_bounds_safety, false)) {
LangOptions::BoundsSafetyNewChecksMaskIntTy NewChecks =
ParseBoundsSafetyNewChecksMaskFromArgs(DriverArgs,
/*DiagnosticsEngine=*/nullptr);
if (NewChecks & LangOptions::BS_CHK_LibCAttributes) {
bool TargetWithoutUserspaceLibc = false;
if (getTriple().isOSDarwin() && !TargetWithoutUserspaceLibc) {
CC1Args.push_back("-D__LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES");
}
}
}
/* TO_UPSTREAM(BoundsSafety) OFF*/
}

/// Take a path that speculatively points into Xcode and return the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// =============================================================================
// Supported target triples
// =============================================================================

// MACRO: -D__LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES
// NO_MACRO-NOT: -D__LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES

// ios
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=NO_MACRO %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=MACRO %s

// macos
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-macos \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=NO_MACRO %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-macos \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=MACRO %s

// macos (legacy `darwin` os name in triple)
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-darwin \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=NO_MACRO %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-darwin \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=MACRO %s

// watchos
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-watchos \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=NO_MACRO %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-watchos \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=MACRO %s

// tvos
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-tvos \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=NO_MACRO %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-tvos \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=MACRO %s

// =============================================================================
// Check option is included in all and option variant without `=all` suffix
// =============================================================================

// ios
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fno-bounds-safety-bringup-missing-checks=all \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=NO_MACRO %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fno-bounds-safety-bringup-missing-checks \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=NO_MACRO %s

// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fbounds-safety-bringup-missing-checks=all \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=MACRO %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fbounds-safety-bringup-missing-checks \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=MACRO %s

// =============================================================================
// Unsupported target triples
// =============================================================================

// linux
// RUN: %clang -Wno-incompatible-sysroot -target x86_64-unknown-linux \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=NO_MACRO %s
// RUN: %clang -Wno-incompatible-sysroot -target x86_64-unknown-linux \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -### -fbounds-safety %s 2>&1 \
// RUN: | FileCheck --check-prefix=NO_MACRO %s
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// This is similar to the driver test of the same name but we verify that
// the macro is actually set/unset as expected.

// =============================================================================
// Supported target triples
// =============================================================================

// ios
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=no_macro -fbounds-safety -fsyntax-only %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=macro -fbounds-safety -fsyntax-only %s

// macos
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-macos \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=no_macro -fbounds-safety -fsyntax-only %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-macos \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=macro -fbounds-safety -fsyntax-only %s

// macos (legacy `darwin` os name in triple)
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-darwin \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=no_macro -fbounds-safety -fsyntax-only %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-darwin \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=macro -fbounds-safety -fsyntax-only %s

// watchos
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-watchos \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=no_macro -fbounds-safety -fsyntax-only %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-watchos \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=macro -fbounds-safety -fsyntax-only %s

// tvos
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-tvos \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=no_macro -fbounds-safety -fsyntax-only %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-tvos \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=macro -fbounds-safety -fsyntax-only %s

// =============================================================================
// Unsupported target triples
// =============================================================================

// linux
// RUN: %clang -Wno-incompatible-sysroot -target x86_64-unknown-linux \
// RUN: -fno-bounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=no_macro -fbounds-safety -fsyntax-only %s
// RUN: %clang -Wno-incompatible-sysroot -target x86_64-unknown-linux \
// RUN: -fbounds-safety-bringup-missing-checks=libc_attributes \
// RUN: -x c -Xclang -verify=no_macro -fbounds-safety -fsyntax-only %s

// =============================================================================
// Check option is included in all and option variant without `=all` suffix
// =============================================================================

// ios
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fno-bounds-safety-bringup-missing-checks=all \
// RUN: -x c -Xclang -verify=no_macro -fbounds-safety -fsyntax-only %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fno-bounds-safety-bringup-missing-checks \
// RUN: -x c -Xclang -verify=no_macro -fbounds-safety -fsyntax-only %s

// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fbounds-safety-bringup-missing-checks=all \
// RUN: -x c -Xclang -verify=macro -fbounds-safety -fsyntax-only %s
// RUN: %clang -Wno-incompatible-sysroot -target arm64-apple-ios \
// RUN: -fbounds-safety-bringup-missing-checks \
// RUN: -x c -Xclang -verify=macro -fbounds-safety -fsyntax-only %s

#ifndef __LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES
// no_macro-error@+1{{expected __LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES macro}}
#error expected __LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES macro
#endif

// macro-no-diagnostics