Skip to content
Open
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
36 changes: 36 additions & 0 deletions regression/cbmc/havoc_object_string_literal/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <wchar.h>

int main()
{
char *s = "abc";
int i = 0;
_Bool c;
char *p = c ? s : (char *)&i;

// String literals cannot be modified, havocking them has no effect.
__CPROVER_havoc_object(s);
__CPROVER_assert(s[0] == 'a', "string literal unchanged");

__CPROVER_havoc_object(p);
__CPROVER_assert(i == 0, "i may be havocked"); // should fail

// A pointer into the middle of a string literal.
__CPROVER_havoc_object(s + 1);
__CPROVER_assert(s[1] == 'b', "offset into string literal unchanged");

// Wide and Unicode string literals are arrays rather than string constants.
const wchar_t *w = L"abc";
__CPROVER_havoc_object((void *)w);
__CPROVER_assert(w[0] == L'a', "wide string literal unchanged");

const unsigned short *u16 = (const unsigned short *)u"abc";
__CPROVER_havoc_object((void *)u16);
__CPROVER_assert(u16[0] == 'a', "UTF-16 string literal unchanged");

const unsigned int *u32 = (const unsigned int *)U"abc";
__CPROVER_havoc_object((void *)(u32 + 1));
__CPROVER_assert(
u32[1] == 'b', "offset into UTF-32 string literal unchanged");

return 0;
}
23 changes: 23 additions & 0 deletions regression/cbmc/havoc_object_string_literal/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CORE
main.c
--no-pointer-check
^EXIT=10$
^SIGNAL=0$
^\[main.assertion.1\] line 12 string literal unchanged: SUCCESS$
^\[main.assertion.2\] line 15 i may be havocked: FAILURE$
^\[main.assertion.3\] line 19 offset into string literal unchanged: SUCCESS$
^\[main.assertion.4\] line 24 wide string literal unchanged: SUCCESS$
^\[main.assertion.5\] line 28 UTF-16 string literal unchanged: SUCCESS$
^\[main.assertion.6\] line 32 offset into UTF-32 string literal unchanged: SUCCESS$
^VERIFICATION FAILED$
--
^warning: ignoring
Invariant check failed
--
__CPROVER_havoc_object applied to a pointer to a string literal used to fail
an invariant in symbolic execution ("Attempted to symex havoc applied to
unsupported expression"). Narrow string literals are string constants, while
wide and Unicode string literals, and the part of a literal that a pointer
into its middle refers to, are arrays. The test uses --no-pointer-check, as
reading a wide string literal currently gives a spurious pointer check failure,
and makes the SMT2 back-end fail an invariant in convert_address_of_rec.
6 changes: 6 additions & 0 deletions src/goto-symex/symex_other.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ void goto_symext::havoc_rec(
{
havoc_rec(state, guard, to_member_expr(dest).struct_op());
}
else if(goto_symex_statet::is_read_only_object(dest))
{
// Read-only objects, such as string literals (narrow ones are string
// constants, wide ones are arrays) and other constants, do not change, so
// there is nothing to havoc.
}
else
{
INVARIANT_WITH_DIAGNOSTICS(
Expand Down
Loading