Skip to content
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

Arrays of objects out of bounds #62

Open
ladisgin opened this issue Mar 9, 2023 · 0 comments
Open

Arrays of objects out of bounds #62

ladisgin opened this issue Mar 9, 2023 · 0 comments
Assignees
Labels
bug Something isn't working с++

Comments

@ladisgin
Copy link
Member

ladisgin commented Mar 9, 2023

Moved from UnitTestBot/UTBotCpp#334

Description
Problem is happen when you try to allocate array of objects with destructor on heap using new[] operator. New allocates 8 + array_size, and writes number of elements in front 8 bytes. Then pointer moves to first element in an array. So, in KLEE there is no information about such prefix, and simple out of bound exception can be missed.

To Reproduce
Consider the following code:

#include <cassert>

int value = 0;

struct A {
  int x;
  A() {}
  A(int x) : x(x) {}
  ~A() { ++value; }
};

int main() {
  A *a = new A[4];
  a[-2] = *(new A(2));
  delete[] a;
  assert(value == 4);
}

Expected behavior
Will be generated 1 successful path, and assertion will be passed.

Actual behavior
1 error path generated and assertion failed.

Visual proofs (screenshots, logs)

KLEE: Using STP solver backend
KLEE: WARNING: undefined reference to function: __gxx_personality_v0
KLEE: WARNING ONCE: Alignment of memory from call "_Znam" is not modelled. Using alignment of 8.
KLEE: WARNING ONCE: Alignment of memory from call "_Znwm" is not modelled. Using alignment of 8.
KLEE: ERROR: exmple.cpp:16: ASSERTION FAIL: value == 4
KLEE: NOTE: now ignoring this error at this location

KLEE: done: total instructions = 212
KLEE: done: completed paths = 1
KLEE: done: generated tests = 1

Compiled and executed with:

clang -emit-llvm -c -g -O0 -Xclang -disable-O0-optnone exmple.cpp
klee exmple.bc 

Additional info
If you will increase argument in line a[-2] = *(new A(2)) up to 5 or greater, you will receive memory out of bound in destructor, that is not an error, that we expect to receive.
This number in prefix means a lot, as code in llvm IR relies on it to call destructors to specified number of objects.
Index -2 is used because sizeof A == 4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working с++
Projects
None yet
Development

No branches or pull requests

2 participants