forked from checkedc/checkedc-clang
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Constrain the most common constructs in non-writable files to not cha…
…nge. (#391) This PR addresses function and variable declarations (because they are the most obvious case and reasonably straightforward) and checked regions (because they came up in some existing regression tests). We'll leave #387 open for the tail of unhandled cases. Also: - When 3C tries to change a non-writable file, issue an error diagnostic (not an assertion failure because there are known unhandled cases) rather than silently discarding the change. - Add a `-dump-unwritable-changes` flag to the `3c` tool to dump the new version of the file when that diagnostic appears. - Add an error diagnostic when 3C tries to change a file under the base dir other than the main file in stdout mode. This is a separate feature (part of #328) but ended up being easy to implement along with the diagnostic for a non-writable file. - Add tests for all the fixes (but not `-dump-unwritable-changes`). - Fix a bug where `-warn-all-root-cause` didn't work without `-warn-root-cause`, because this affected one of the new tests. The use of `-warn-all-root-cause` without `-warn-root-cause` in the affected test will serve as a regression test for this fix as well. Fixes part of #387 and a few unrelated minor issues.
- Loading branch information
1 parent
128c8b8
commit ec3bf4c
Showing
14 changed files
with
252 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
clang/test/3C/base_subdir/canwrite_constraints_function_and_variable.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Test that non-canWrite files are constrained not to change so that the final | ||
// annotations of other files are consistent with the original annotations of | ||
// the non-canWrite files. The currently supported cases are function and | ||
// variable declarations and checked regions. | ||
// (https://github.com/correctcomputation/checkedc-clang/issues/387) | ||
|
||
// TODO: When https://github.com/correctcomputation/checkedc-clang/issues/327 is | ||
// fixed, replace the absolute -I option with a .. in the #include directive. | ||
// | ||
// TODO: Windows compatibility? | ||
|
||
// "Lower" case: -base-dir should default to the working directory, so we should | ||
// not allow canwrite_constraints_function_and_variable.h to change, and the | ||
// internal types of q and the return should remain wild. | ||
// | ||
// RUN: cd %S && 3c -addcr -extra-arg=-I${PWD%/*} -output-postfix=checked -warn-all-root-cause -verify %s | ||
// RUN: FileCheck -match-full-lines -check-prefixes=CHECK_LOWER --input-file %S/canwrite_constraints_function_and_variable.checked.c %s | ||
// RUN: test ! -f %S/../canwrite_constraints_function_and_variable.checked.h | ||
// RUN: rm %S/canwrite_constraints_function_and_variable.checked.c | ||
|
||
// "Higher" case: When -base-dir is set to the parent directory, we can change | ||
// canwrite_constraints_function_and_variable.h, so both q and the return should | ||
// become checked. | ||
// | ||
// RUN: cd %S && 3c -addcr -extra-arg=-I${PWD%/*} -base-dir=${PWD%/*} -output-postfix=checked %s | ||
// RUN: FileCheck -match-full-lines -check-prefixes=CHECK_HIGHER --input-file %S/canwrite_constraints_function_and_variable.checked.c %s | ||
// RUN: FileCheck -match-full-lines -check-prefixes=CHECK_HIGHER --input-file %S/../canwrite_constraints_function_and_variable.checked.h %S/../canwrite_constraints_function_and_variable.h | ||
// RUN: rm %S/canwrite_constraints_function_and_variable.checked.c %S/../canwrite_constraints_function_and_variable.checked.h | ||
|
||
#include "canwrite_constraints_function_and_variable.h" | ||
|
||
int *bar(int *q) { | ||
// CHECK_LOWER: int *bar(int *q : itype(_Ptr<int>)) : itype(_Ptr<int>) { | ||
// CHECK_HIGHER: _Ptr<int> bar(_Ptr<int> q) { | ||
foo(q); | ||
return foo_var; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// An example of a case (typedefs) that is not yet handled by the canWrite | ||
// constraints code and causes 3C to generate a change to an unwritable file. | ||
// Test that 3C generates an error diagnostic. | ||
// (https://github.com/correctcomputation/checkedc-clang/issues/387) | ||
|
||
// TODO: Ditto the TODO comments from | ||
// canwrite_constraints_function_and_variable.c re the RUN commands. | ||
// RUN: cd %S && 3c -addcr -extra-arg=-I${PWD%/*} -verify %s | FileCheck -match-full-lines %s | ||
|
||
// expected-error@unwritable_typedef.h:1 {{3C internal error: 3C generated changes to this file even though it is not allowed to write to the file}} | ||
// expected-note@unwritable_typedef.h:1 {{-dump-unwritable-changes}} | ||
|
||
#include "unwritable_typedef.h" | ||
|
||
foo_typedef p = ((void *)0); | ||
|
||
// To make sure we are testing what we want to test, make sure bar is rewritten | ||
// as if foo_typedef is unconstrained. If foo_typedef were constrained, we'd | ||
// expect bar to be rewritten differently. | ||
int *bar(void) { | ||
// CHECK: _Ptr<int> bar(void) _Checked { | ||
return p; | ||
// Make sure 3C isn't inserting a cast or something clever like that. | ||
// CHECK: return p; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This subdirectory is used as the -base-dir by tests that want to have files | ||
outside the -base-dir. |
Oops, something went wrong.