Граф коммитов

264 Коммитов

Автор SHA1 Сообщение Дата
Sam Elliott 3febc2c49e
Technical Report Number 2 (#248)
The overall output of my work in 2017.
2018-01-22 13:22:39 -08:00
David Tarditi 57fbaf62e4
Error messages for problems involving return statements. (#247)
C programs can have problems with return statements that would result in undefined or incorrect program behavior. C compilers typically warn about these problems (in other languages, these would typically be compile-time errors because of the severity of the problem). For Checked C, these problems need to result in errors when they occur in a checked scope or involve bounds or check pointer types. This
change adds tests for these problems.
2018-01-09 14:42:51 -08:00
David Tarditi 78f04d0610
Test assignment to a variable with a bounds-safe interface. (#245)
Reading variables with bounds-safe interfaces works fine.  Writing
variables with bounds-interfaces caused a compiler crash. Add a
simple test for writing a variable.  We should add more tests
for writes at bounds-safe interfaces eventually.

I placed this test in a new subdirectory runtime_operations. This
will contain tests for checked type operations that are done at
runtime, but don't do dynamic runtime checks.

In this case, there is an implicit cast inserted at bounds-safe interfaces.
We want to make sure those casts operate correctly.
2017-12-22 11:19:18 -05:00
David Tarditi 84bc69b541
Remove -fcheckedc-extension flag. (#244)
The -fcheckedc-extension flag is now set by default for C files in the Checked C version of clang.  Remove the flag from the command lines for the test harness.

Also update a few error messages that changed after fixing bugs in the compiler.  The bugs were found after building the clang test suite with -fcheckedc-extension on by default.
2017-12-21 19:28:38 -05:00
David Tarditi bc45607280 Update error message for RC 5.01. 2017-12-06 15:36:20 -08:00
David Tarditi f63a82dcfe
Allow writing a 0 at the upper bound of a null-terminated array. (#242)
This change addresses the suggestion from @mwhicks1 in issue #235 that we relax the bounds checking for null-terminated arrays to allow a write of 0 at the upper bound of a null-terminated array.  There will be a matching pull request in the checked-clang repo for compiler change.  

- Update the description of bounds checking in the Checked C specification.
- Add tests to nullterm_pointers.c that check this behavior.
-  Update the sample code in string-helpers.c to delete some special-case code in the "squeeze" function.  This function removes all occurrences of a character from a string.  If no characters are removed, it redundantly overwrites the existing null-terminator.  Before, this had to be special-cased to avoid an out-of-bounds write.   I also updated the sample code to avoid declaring default bounds of count(0) and clarified the comments.

I ran into a subtlety when revising the specification.  When we allow a 0 to be written at the upper bound, we have to expand the allowed range of memory to include the memory at the upper bound.  It is possible for even this expanded range to be empty.   The specification is careful to not allow a write of 0 in this situation., by including a comparison of the pointer address against the lower bound.
2017-11-29 16:22:34 -05:00
David Tarditi 4e765fce8a
Update error messages to use the term "unknown bounds". (#243)
We've changed the compiler to use the term "unknown bounds" in error messages
instead of "no bounds" because "no bounds" is ambiguous. It could mean
unbounded or unknown bounds.   Update the error messages that are checked
in tests.
2017-11-29 14:54:19 -05:00
David Tarditi 6160b43f43
Warnings about unprovable bounds declarations in checked scopes. (#240)
The compiler now always warns when it cannot prove a bounds
declaration true or false in a checked scope.   This adds warnings to tests
where this is the case.  Most of these warnings will disappear when we extend
bounds declaration checking to include facts about equality of
variables/expressions after assignments.

The compiler also understands equivalence of base expressions for
constant-sized regions better.  This lets the compiler deduce that
`*(_Dynamic_bounds_cast<array_ptr<int>>(r, 1) + 2) = 4` will always
fault at runtime.
2017-11-15 13:53:35 -08:00
Sam Elliott b49aa60fc7
Make Fortify-source definitions more fine-grained (#239)
Sometimes, despite _FORTIFY_SOURCE being nonzero, there may not be a #define that overwrites a function definition, so we need to cope with this in these definitions. This does so.
2017-11-15 12:38:15 -08:00
David Tarditi ff6965abb4
Compiler now warns about some out-of-bounds memory accesses. (#238)
Update dynamic tests of runtime bounds checking to suppress these warnings.  We don't expect to see warnings for these tests and turn warnings into errors.

For bounds declaration checking, turn TODO's for expected warnings into pattern matches for the expected warnings.
2017-11-13 08:27:41 -08:00
David Tarditi 63d7f11474
Add error messages produced by bounds declaration checking (#237)
We are updating the compiler to do bounds declaration checking for constant-sized ranges (https://github.com/Microsoft/checkedc-clang/pull/414).   This causes new error messages to be produced for a number of tests.  For example, tests for typechecking might pass typechecking (as expected), but then fail bounds declaration checking..  

Update the tests with the expected error messages from clang (most of these errors were expected once enough bounds declaration checking came on-line and were flagged by TODO comments).

As an FYI, there are additional bounds declaration checking tests in the Checked C clang repo.  These contain detailed checks of the expected explanations, and are not placed in the Checked C repo.

The bounds declaration checking detects and gives errors for the bogus C-style casts described in issue #232.
2017-11-11 05:56:59 -08:00
Sam Elliott f534fb22e8
Add _FORTIFY_SOURCE support for string.h and stdio.h (#234)
This adds the fortified declarations, in line with the specifications within clang and the gcc documentation here: https://gcc.gnu.org/onlinedocs/gcc-6.3.0/gcc/Object-Size-Checking.html#Object-Size-Checking

This simplifies the checks for fortify source, and makes the tests more robust.

There is also the addition of a posix header, unistd.h, as a stopgap measure.

Tested: Mac OS X, Linux, Windows.
2017-11-07 09:47:21 -08:00
David Tarditi 261aacbce9
Use null-terminated array pointers in checked wrappers for C standard libraries (#229)
This change updates our checked wrappers for C standard libraries to use null-terminated array pointers for strings.
- I expanded the list of string functions and had to disable more optimized macros for GCC.
- In cases where I could make some parameters a function checked, but not other parameters checked,
I went ahead and did that.  This include functions such as `strcat` and `strncat`.   The fact that the destination buffer has an unchecked pointer type should be a warning to programmers trying to use them.
- For `strncat`, we have no way of expressing the bounds requirement in checked code, because the bounds requirement involves a function call.

Testing:
- Passed automated testing, including LNT testing.
2017-11-04 23:05:36 -07:00
David Tarditi 96d639b1e3
Update overlooked test for default bounds for null-terminated array pointers. (#225) 2017-11-03 14:24:16 -07:00
David Tarditi 22c3006df3
Add another example of using strings and add string support to stdio_checked.h. (#222)
The example is adapted from a simple "pattern finding" program from "The C Programming Language, Second Edition", by Kernighan and Ritchie.  The example uses only checked code.    The example does output using fputs; I updated fputs and the rest of stdio_checked.h with annotations for strings.
2017-11-03 12:20:21 -07:00
David Tarditi 166f9f8940
Test default bounds for null-terminated array pointers (#220)
Null-terminated array pointers have a default bounds of count(0).   This adds tests of bounds checking and type checking for null-terminated array pointers with programmer-specified bounds and default bounds.   This change matches a corresponding clang compiler change (https://github.com/Microsoft/checkedc-clang/pull/406).

There need to be additional tests added of struct members with default bounds of count(0).  

When bounds are not declared for an nt_array_ptr variable or member, the
bounds are inferred to be count(0).  This differs from array_ptr, where the
bounds are assumed to be bounds(unknown).

nt_array_ptr values returned by pointer dereference or array subscript
operations are also assumed to have count(0).   nt_array_ptr values
stored via pointer dereference or array_subscript operations must
also imply bounds of count(0).

This change adds tests that check that bounds inference is inferring
default bounds as expected.  Because we have weak static checking of bounds
declarations, we only check that expressions involving nt_array_ptr variables
have known bounds as expected.

* Test redeclarations that provides same as default bounds.

* Add missing initializers discovered by setting default bounds earlier.
2017-11-03 12:10:46 -07:00
David Tarditi eebfc64ee1
Add examples of using null-terminated strings. (#219)
This adds examples of using null-terminated strings adapted from the "The C Programming Language, Second Edition" by Kernighan and Ritchie. The code illustrates some basic aspects of using strings, as well as the sometimes trickier aspects of writing to a string.

I plan to clean up the code once the compiler change for making count(0) be the default bounds for nt_array_ptr is complete.



I plan to clean up the code once the compiler change for making count(0) be the default bounds for nt_array_ptr is complete.
2017-11-01 12:20:43 -07:00
David Tarditi 3d8024ca7a Rename bounds(none) to bounds(unknown). (#213)
This change goes with a matching compiler change.   Update the Checked C tests to match the specification.
2017-10-24 11:06:07 -07:00
David Tarditi 6cd056bb8b Update error messages that include checked array type names. (#212)
The compiler was using the keywords `checked` and `nt_checked` when it should have used the full names `_Checked` and `_Nt_checked`.
2017-10-23 15:21:55 -07:00
David Tarditi fb1fc3dd32 Test bounds checking for null-terminated pointers. (#211)
Checked C allows reads at exactly the upper bound for null-terminated pointers
so that the potential terminating value can be examined.

This change adds new dynamic tests that check  that the memory location at
the upper bound is accessible for null-terminated array pointers, and not
accessible for non-null-terminated array pointers.
2017-10-23 14:10:56 -07:00
David Tarditi ac4f72f849 Replace bounds(none) with bounds(unknown) (#209)
The bounds expression bounds(none) has a potentially ambiguous English meaning.   The bounds could be unbounded or be unknown.  This replaces the bounds expression bounds(none) with bounds(unknown) in the specification.  This is mostly a search-and-replace of the boundsnone macro.  I also updated the text that defines the meaning of the bounds expression.
2017-10-19 14:29:48 -07:00
David Tarditi 3ae1fa1469 Include null terminators in the number of elements for null-terminated arrays. (#210)
The Checked C specification proposed omitting the null terminator from the number of elements in a null-terminated array.   This would allow the count of elements to be identical for both the array and the bounds for an `nt_array_ptr`.  However, this is not how null-terminated string literals work in C.    String literals have an array type that includes the null terminator element in the size of the array.

To match existing C behavior, we now include the null terminator in the size of null-terminated checked arrays.  This updates the specification accordingly.
2017-10-19 14:28:38 -07:00
David Tarditi 4764ab93c7 Test strings with checked types and initialized checked data. (#208)
This change corresponds to a matching compiler that adds support for string literals with checked types and completes support for initialized checked data.   The language rules for string and array literals are discussed in issue #207 (they haven't been added to the Checked C specification yet).

The compiler change also improves error messages for types that occur syntactically in expressions and are used in checked scopes.  Those types must be checked and everything they use must be checked too.   This change updates checked_scope_basic.c to use new error messages.

Testing:
- Passes automated testing with the updated compiler.
2017-10-17 12:48:08 -07:00
David Tarditi 7f986ec2c8 Disallow array_ptrs to function types. (#204)
Update the specification to disallow array_ptrs of function types. Bounds checking pointers to function types does not make sense because functions do not have sizes in C.  This addiresses issue #34.

Also correct the capitalization of the _Nt_array_ptr keyword in the specification.
2017-10-10 16:15:11 -07:00
David Tarditi e4690c62f7 Test parsing and type checking of null-terminated arrays and pointers. (#202)
This change adds tests for parsing and typechecking the new Checked C null-terminated array and pointer types.  This matches a corresponding Checked C clang compiler pull request (https://github.com/Microsoft/checkedc-clang/pull/394).  The changes are integrated into existing test cases for checked arrays and array_ptrs.  

The tests for parsing are straightforward.  The new keywords `nt_checked` and `nt_array_ptr` can be used in place of the `checked` and `array_ptr` keywords.  For type checking, we add tests for building the new types. We check that null-terminated arrays and pointers can only be built from integer or pointer types (the assumption is that 0 is the null-terminator value, so only types for which 0 is a valid value can be used).

We also add tests that cover implicit conversions at assignments, function calls, and conditional expressions.  Nt_array_ptrs can be converted to array_ptrs and ptrs, but not the reverse.  The problem is that array_ptrs or ptrs may not point to null-terminated data, or, if they do, there may be an alias that permits the null-terminator to be overwritten.  We also add tests that cover uses of operators with the new types.

The tests include positive cases (where no error is expected) and many more negative cases, where the compiler is expected to issue an error. To avoid unnecessary source code changes that would obscure the new tests, I avoided renumbering temporary variables. I added letter suffixes to existing variables instead.

The compiler change disallows array_ptrs to function types (https://github.com/Microsoft/checkedc/issues/34), so we add tests of that too.  Functions have indeterminate size, so bounds checking does not make sense for them.  We allow array_ptrs to ptrs to function types.
2017-10-10 15:29:25 -07:00
David Tarditi 92719992bc Fix uninitialized variables in static_check_bounds_cast.c (#203)
We should not have uses of uninitialized variables in static_check_bounds_cast.c because that is undefined behavior.
2017-10-10 15:28:29 -07:00
David Tarditi 13d1169d9c Test bounds checking for variables with bounds-safe interfaces. (#200)
Variables and members with unchecked types may have bounds-safe interfaces.
When they are used in checked scopes, they should be treated as though they
have checked types.  This means that accesses to memory using pointers
stored in those variables or member should be bounds checked.

The changes adds tests of bounds checking of global and local variables with
bounds-safe interfaces and struct members with bounds-safe interfaces.  This
completes the work for issue #339.
2017-09-28 15:52:55 -07:00
David Tarditi acac9e402d Test use of declared bounds for arrays if present. (#199)
Add tests of uses of variables and fields with array types where
bounds are also declared.  The declared bounds should override the bounds
based on the array type.  This matches a corresponding Checked C compiler change.

These tests check that bounds checking is being done against the declared bounds.
For arrays with known size, the declare bounds should be smaller than the bounds
based on the type.   Accessing an element above the declared bounds should fail.

Also test variable-length arrays at the ends of structs.  Without using the declared
bounds, they have unknown bounds and can't be accessed in checked code.
2017-09-28 15:43:28 -07:00
David Tarditi cbbc6ceb64 Typechecking uses of declarations with bounds-safe interfaces in checked scopes (#198)
This change adds tests of type checking uses of declarations with bounds-safe interfaces in checked scopes.  The Checked C specification says that such uses shall be treated as having checked types.  This change adds tests that check this.   It matches the compiler change in https://github.com/Microsoft/checkedc-clang/pull/379.  

It adds a new file typechecking\checked_scope_interfaces.c that contains tests of the uses of bounds-safe interfaces in checked scopes.   The file systematically combines different kinds of declarations with bounds-safe interfaces.  It varies the types, starting with simple types (pointer to scalar types) and then looking at more complex constructed types (pointer to pointers, pointers to function pointers, and arrays of function pointers).   It also includes some stress tests that combine bounds-safe interfaces, function pointer types, and typedefs.    

We now allow casts from array_ptr<void> to other checked pointer types.   This change updates tests to reflect this change.

It refactors checked_scope.c.  Most of the code moves to checked_scope_basic.c, which focuses on testing uses of checked and unchecked types in checked scopes.   Some tests that are redundant with other existing tests are deleted.   The code specific to bounds-safe interfaces move to checked_scope_interfaces.c.

Avoid using unchecked pointers to structs in tests of bounds checking - use checked pointers instead.
2017-09-26 14:20:40 -07:00
David Tarditi 1b5fcf8a00 Add tests that check that call arguments have bounds. (#196)
When a function parameter has bounds, corresponding call arguments should have bounds.  This adds tests that check that.   This corresponds to the compiler changes for https://github.com/Microsoft/checkedc-clang/issues/373,
2017-09-20 17:17:53 -07:00
David Tarditi 3fb37d718c Add variant of array_ptr for pointers to null-terminated arrays. (#192)
This change adds a new type `nt_array_ptr` that is a variant of `array_ptr` for pointers to null-terminated arrays.   The change:
- describes the new type in core-extensions.tex.   
- describes a new checked null-terminated array type go with `nt_array_ptr`
- describes the runtime bounds checking rules for nt_array_ptr.

For `nt_array_ptr` we allow inferred bounds if bounds are not declared.     Inference can widen bounds when it sees non-null memory access.   This raises the problem that memory accesses could depend on bounds that are not written down in the program.  The proposal is that any such accesses to memory must be provably in-bounds at compile time.

Most of the rules later in the specification for `array_ptr` apply to `nt_array_ptr`.    I initially started down the route of updating all occurrences of `array_ptr` to say "or `nt_array_ptr`".   I decided that was resulting in lots of changes that didn't help understanding much and made the text longer.   There is a paragraph in the description of `nt_array_ptr` that says that the rules for `array_ptr` usually apply to `nt_array_ptr` and that we mention `nt_array_ptr` only when the rules are different.

This change also cleans up some aspects of the spec:
- Remove the text that says bounds-checking is done for &e, if e is a dereference or subscript operation.  This is incorrect and doesn't match what the implementation does.
- Makes the description of bounds checks more precise, by describing the use of temporaries instead of expressions during bounds checking (the expression being bounds checked is not re-evaluated).
- Revise the section describing the deferral of the evaluation of bounds expression.

Still to do (in another pull request):
- Update the rules for conversions between checked pointer types.
- Describe the analysis that widens bounds for 'nt_array_ptr`.
2017-09-11 10:22:17 -07:00
David Tarditi ccbdf1cfb4 Test returning a checked pointer from a function with an interop type. (#194)
Add tests for returning checked pointers from functions with bounds-safe
interfaces for their return values.  Modify an existing test to allow this
behavior.
2017-09-06 09:19:07 -07:00
Sam Elliott 46de88f22f Correct Interoperation type of stdlib.h's free(void*) (#190)
The important part of `free` is that you're passing an in-bounds pointer to an object to be free-ed. Given how we infer bounds for both array_ptrs and ptrs, require subsumption for function arguments, and that the smallest range possible for a pointer is `byte_count(1)`, this is the correct interface type rather than one that explicitly mentions array_ptr or ptr. 

We also change the interoperation type of the pointer parameter of `void *realloc(void*, size_t)` because it can also free memory. 

We add a set of tests that ensure these interface types for malloc, free, realloc and alloc_aligned operate with each other for every kind of pointer: unchecked, ptr and array_ptr.
2017-08-30 14:32:20 -07:00
David Tarditi 6eb08348ba Update tests involving void pointers. (#191)
- Update tests to allow conversions from any kind of checked pointer to
  any kind of checked void pointer (ptr<void> and array_ptr<void>).
- Test that implicit conversions between void pointers and checked function
  pointers are not allowed.  This is a common C extension that should
  not be allowed for checked function pointers.
2017-08-30 13:48:44 -07:00
David Tarditi 66527b795a Revamp error messages for issues in checked scopes involving unchecked pointers nested in other types. (#189)
Checked C clang pull request https://github.com/Microsoft/checkedc-clang/pull/363 addresses the problem that bounds-safe interfaces on function pointers were not being used during type checking.  It substantially revised the error messages for cases where the term "unchecked type" might be unclear to programmers (for example, a function or array type that uses an unchecked type). 

This updates the error messages in the existing Checked C tests.  It also adds tests of typecheckng function pointer types involving bounds-safe interfaces.
2017-08-29 15:19:00 -07:00
David Tarditi e51720c47a Add test case for K&R definition preceded by prototype. (#187) 2017-08-08 16:11:58 -07:00
David Tarditi 10da185e53 Additional tests for generic and no-prototype functions. (#185)
- Test that generic no-prototype functions cannot be declared.
- Test that generic K&R old-style functions cannot be declared.
- Test generic functions applied to an empty type argument list.  Make
  sure an error is emitted if type arguments are expected.
- Stop capitalizing the first word of some error messages for generic
  functions.
- Stop checking notes issued by clang in Checked C language tests.  These
  are implementation-specific details that don't belong in language tests.
- Improve tests that K&R old-style functions can be declared or used within
  checked blocks.  We weren't testing that K&R style functions cannot
  be declared within checked blocks.
2017-08-02 16:50:22 -07:00
David Tarditi 72b6ba50b0 Update stdchecked.h for bounds cast operators. (#186) 2017-08-01 17:15:14 -07:00
Jay Lim acb74810d9 Test case changes to accomodate Incomplete Type of Type Variable (#184)
* Adding test files for _For_any specifier and type variable declarations.

* Adding test case to generic Function Call Info

* The comments were wrong in the newly created test case.

* Fixing more errors due to the fact that Type Variables are now incomplete type
2017-07-24 13:15:41 -07:00
Jay Lim 9700315dfd Test cases for generating correct error message when the number of type arguments are not correct. (#183)
* Adding test files for _For_any specifier and type variable declarations.

* Adding test case to generic Function Call Info

* The comments were wrong in the newly created test case.
2017-07-20 16:31:51 -07:00
Jay Lim bc6a06e773 Change function declaration to be prototyped. (#182)
With the introduction of type variable types in clang, it is important to make sure that generic functions are a FunctionProtoType. We did not allow generic functions to be FunctionNoProtoType. In order for the tests to work correctly, I had to add "void" in () if there are no parameters.
2017-07-11 13:26:18 -07:00
Sam Elliott 4d9dc92673 Updates Tests for Improved Bounds Inference (#180)
These tests go along with checkedc-clang cd04c02dd395d776b74e6c463af58384cdd5b7f2
2017-07-06 10:20:19 -07:00
David Tarditi f11e01597b Add tests for lexical equality of expressions. (#178)
As part of statically checking bounds declarations, we need to check symbolic equality of expressions and to lexicographically order expressions (for sorting commutative operands).  This implements language-level tests of bounds declarations that indirectly cover lexical equality/inequality.
2017-07-01 00:25:26 -07:00
Jay Lim f0b5d5d5d8 Fix test error after generic func call (#179)
* Adding test files for _For_any specifier and type variable declarations.

* Fixing an error in forany_parsing.c This is caused because now the generic function call expects to see type lists
2017-06-30 12:55:51 -07:00
Jay Lim 281a80978d Test cases for parsing generic function calls (#175)
* Adding test files for _For_any specifier and type variable declarations.

* These two files tests for parsing generic function calls and any errors that clang may encounter

* Editing parsing error testing in accordance to coding changes in checkedc-clang

* Moving a couple test cases to checkedc-clang since it tests for specifics of how clang is tested.
2017-06-30 09:59:19 -07:00
David Tarditi 6a33652c9c Stop generating LLVM IR as part of testing forany_parsing. (#177)
For_any parsing is generating LLVM IR files as part of test runs. This causes automation testing to fail because it creates the file in a source directory. The next automation run fails because it finds a file with no RUN line in it in the test directory. The fix is to not generate LLVM IR files.
2017-06-20 00:28:46 -07:00
Jay Lim 1728c482b1 Adding test files for _For_any specifier and type variable declarations. (#172) 2017-06-19 11:15:20 -07:00
Sam Elliott 02819ae56e Correct Conditionals for apple system header support (#174)
I should not be trusted with De Morgan's Laws, turns out I'll break them.

`!a && b` is not equivalent to `!(a && b)`.

This should make Ptrdist/anagram work on Linux again, I tested it on my WSL installation
2017-06-16 16:18:17 -07:00
David Tarditi b62a2572c0 Fix stdio_checked.h for Windows. (#170)
Stdio_checked.h is failing to compile on Windows (issue #169).  Bounds-safe
interfaces were added for stdin, stdout, and stderr.  These identifiers only
have to be expressions with type FILE *.  On Windows, they are function calls,
so we add a bounds-safe interface for the function being called.
2017-06-08 07:09:41 -07:00
Sam Elliott 57e98ccfc4 Header Fixups from Benchmark Conversion (#167)
A Variety of changes

- Removing some `_Unchecked` annotations
- Adding interop type to `__builtin_object_size`
- Adding interop types for `stdin` `stdout` and `stderr`
- Adding interop declaration for `fputc`
2017-06-05 22:12:38 -07:00