Skip to content

Commit 221f009

Browse files
danliew-appledelcypher
authored andcommitted
[BoundsSafety] Add build settings to control the bounds check mode for -fbounds-safety
This patch introduces two new build settings The first is `CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS`. This setting can be used to control which new bounds checks are enabled. It takes two special values: 1. `none` - This disables all the new bounds checks. This corresponds to `-fno-bounds-safety-bringup-missing-checks`. 2. `default` - This will use the compiler default (i.e. the build system won't pass the `-fbounds-safety-bringup-missing-checks=` flag). If the build setting value is not any of the special values it is passed directly as an argument to `-fbounds-safety-bringup-missing-checks=`. This allows enabling a specific set of checks. E.g.: ``` CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS=access_size,return_size ``` would pass `-fbounds-safety-bringup-missing-checks=access_size,return-size`. The second build setting is `CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS_OPT_OUTS`. This setting is intended to complement the previous build setting by providing a way to opt out of specific bounds checks. E.g.: ``` CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS=batch_0 CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS_OPT_OUTS=access_size ``` This is equivalent to ``` -fbound-safety-bringup-missing-checks=batch_0 -fno-bounds-safety-bringup-missing-checks=access_size ``` This opts the compilation unit into all checks in the `batch_0` group except the `access_size` bounds check. These new build settings only apply when all of the following are true: * `CLANG_ENABLE_BOUNDS_SAFETY` or `CLANG_ENABLE_BOUNDS_ATTRIBUTES` is enabled. * For C source files The existing `boundsSafetyCLanguageExtension` test case has been modified to test the behavior of these new flags. rdar://161599307
1 parent 9ff5887 commit 221f009

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

Sources/SWBUniversalPlatform/Specs/Clang.xcspec

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,44 @@
543543
};
544544
// Hidden.
545545
},
546+
{
547+
Name = "CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS";
548+
Type = Enumeration;
549+
FileTypes = (
550+
"sourcecode.c.c",
551+
);
552+
Values = (
553+
"none",
554+
"default",
555+
);
556+
DefaultValue = "default";
557+
CommandLineArgs = {
558+
"none" = ( "-fno-bounds-safety-bringup-missing-checks" );
559+
// Use the compiler default
560+
"default" = ();
561+
// E.g. `batch_0` or `all` or comma separated lists of checks to enable
562+
"<<otherwise>>" = ( "-fbounds-safety-bringup-missing-checks=$(value)" );
563+
};
564+
565+
Condition = "$(CLANG_ENABLE_BOUNDS_ATTRIBUTES) || $(CLANG_ENABLE_BOUNDS_SAFETY)";
566+
// Hidden
567+
},
568+
{
569+
Name = "CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS_OPT_OUTS";
570+
Type = String;
571+
FileTypes = (
572+
"sourcecode.c.c",
573+
);
574+
DefaultValue = "";
575+
CommandLineArgs = {
576+
"" = ();
577+
// Comma separated list of checks to disable
578+
"<<otherwise>>" = ( "-fno-bounds-safety-bringup-missing-checks=$(value)" );
579+
};
580+
Condition = "$(CLANG_ENABLE_BOUNDS_ATTRIBUTES) || $(CLANG_ENABLE_BOUNDS_SAFETY)";
581+
AppearsAfter = "CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS";
582+
// Hidden
583+
},
546584
{
547585
Name = "CLANG_ENABLE_APP_EXTENSION";
548586
Type = Boolean;

Tests/SWBTaskConstructionTests/TaskConstructionTests.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9185,6 +9185,72 @@ fileprivate struct TaskConstructionTests: CoreBasedTests {
91859185
}
91869186
}
91879187
}
9188+
9189+
// Test missing check build settings
9190+
let boundsSafetyMissingChecksSetting = "CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS"
9191+
let enableBoundsAttributesSetting = "CLANG_ENABLE_BOUNDS_ATTRIBUTES"
9192+
let boundsSafetyMissingChecksOptOutsSetting = "CLANG_BOUNDS_SAFETY_BRINGUP_MISSING_CHECKS_OPT_OUTS"
9193+
9194+
for (enableBoundsSafetyBuildSetting, enableBoundsSafetyFlag) in [
9195+
(enableBoundsSafetySetting,"-fbounds-safety"),
9196+
(enableBoundsAttributesSetting, "-fbounds-attributes")] {
9197+
9198+
// Maps an array of build overrides to a closure that tests properties of the build overrides
9199+
let missingChecksOverrides = [
9200+
[ enableBoundsSafetyBuildSetting: "YES", boundsSafetyMissingChecksSetting: "none"]: { (task: any PlannedTask) in
9201+
task.checkCommandLineContains(["-fno-bounds-safety-bringup-missing-checks"])
9202+
},
9203+
9204+
// Default behavior is not pass the compiler flag and thus use the compiler default
9205+
[ enableBoundsSafetyBuildSetting: "YES", boundsSafetyMissingChecksSetting: "default"]: { (task: any PlannedTask) in
9206+
task.checkCommandLineDoesNotContain("-fno-bounds-safety-bringup-missing-checks")
9207+
task.checkCommandLineDoesNotContain("-fbounds-safety-bringup-missing-checks")
9208+
},
9209+
[ enableBoundsSafetyBuildSetting: "YES"]: { (task: any PlannedTask) in
9210+
task.checkCommandLineDoesNotContain("-fno-bounds-safety-bringup-missing-checks")
9211+
task.checkCommandLineDoesNotContain("-fbounds-safety-bringup-missing-checks")
9212+
},
9213+
9214+
9215+
[ enableBoundsSafetyBuildSetting: "YES", boundsSafetyMissingChecksSetting: "batch_0"]: { (task: any PlannedTask) in
9216+
task.checkCommandLineContains(["-fbounds-safety-bringup-missing-checks=batch_0"])
9217+
task.checkCommandLineDoesNotContain("-fno-bounds-safety-bringup-missing-checks")
9218+
},
9219+
[ enableBoundsSafetyBuildSetting: "YES", boundsSafetyMissingChecksSetting: "access_size,return_size"]: { (task: any PlannedTask) in
9220+
task.checkCommandLineContains(["-fbounds-safety-bringup-missing-checks=access_size,return_size"])
9221+
task.checkCommandLineDoesNotContain("-fno-bounds-safety-bringup-missing-checks")
9222+
},
9223+
9224+
// Opt-outs
9225+
[ enableBoundsSafetyBuildSetting: "YES",
9226+
boundsSafetyMissingChecksSetting: "batch_0",
9227+
boundsSafetyMissingChecksOptOutsSetting: "access_size"]: { (task: any PlannedTask) in
9228+
task.checkCommandLineContains([
9229+
"-fbounds-safety-bringup-missing-checks=batch_0",
9230+
"-fno-bounds-safety-bringup-missing-checks=access_size"])
9231+
},
9232+
[ enableBoundsSafetyBuildSetting: "YES",
9233+
boundsSafetyMissingChecksSetting: "batch_0",
9234+
boundsSafetyMissingChecksOptOutsSetting: "access_size,return_size"]: { (task: any PlannedTask) in
9235+
task.checkCommandLineContains([
9236+
"-fbounds-safety-bringup-missing-checks=batch_0",
9237+
"-fno-bounds-safety-bringup-missing-checks=access_size,return_size"])
9238+
},
9239+
]
9240+
9241+
for (overrides, checkOverrideProperties) in missingChecksOverrides {
9242+
await tester.checkBuild(BuildParameters(configuration: "Debug", overrides: overrides), runDestination: .macOS, fs: fs) { results -> Void in
9243+
results.checkTarget("AppTarget") { target -> Void in
9244+
results.checkTask(.matchTarget(target), .matchRuleType("CompileC"), body: {task in
9245+
// Check the overrides contains -fbounds-safety or -fbounds-attributes
9246+
task.checkCommandLineContains([enableBoundsSafetyFlag])
9247+
// Check properties specific to this array of overrides
9248+
checkOverrideProperties(task)
9249+
})
9250+
}
9251+
}
9252+
}
9253+
}
91889254
}
91899255
}
91909256

0 commit comments

Comments
 (0)