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

Dynamic memory allocation using new doesn't seem to work #104

Closed
albertnetymk opened this issue Dec 1, 2020 · 3 comments
Closed

Dynamic memory allocation using new doesn't seem to work #104

albertnetymk opened this issue Dec 1, 2020 · 3 comments

Comments

@albertnetymk
Copy link

Running it using nidhuggc -std=c++17 -pthread -- --sc test.cpp. The stack allocation version works fine, while the use of new causes segfault, with the warning, WARNING: Calling unknown external function _Znwm as blackbox.

#include <pthread.h>
#include <new>

using namespace std;

namespace {

struct Node {
  pthread_mutex_t _m;

  Node() {
    pthread_mutex_init(&_m, nullptr);
  }
};

void my_lock(Node* n) {
  pthread_mutex_lock(&n->_m);
}
void my_unlock(Node* n) {
  pthread_mutex_unlock(&n->_m);
}

Node* head;

void t0()
{
  my_lock(head);
  my_unlock(head);
}

} // namespace

int main()
{
  // head = new Node(); // segmentation fault
  Node n;
  head = &n;

  constexpr auto n_thread = 2;
  pthread_t threads[n_thread];
  for (auto i = 0; i < n_thread; ++i) {
    pthread_create(
        &threads[i],
        nullptr,
        [](void*) -> void* {t0(); return nullptr;},
        nullptr);
  }
  for (auto i = 0; i < n_thread; ++i) {
    pthread_join(threads[i], nullptr);
  }

  return 0;
}
@margnus1
Copy link
Contributor

margnus1 commented Dec 3, 2020

Thanks, Albert!

Right now, nidhuggc only supports the parts of the c and c++ standard libraries that inline (plus a short list of extra functions from the c standard library). In order to use more library functions, these have to be compiled to LLVM IR and linked to the program to be tested. Klee solves this by bundling its own C and C++ libraries which it builds LLVM IR for. I'd like Nidhugg to do something similar, but it doesn't yet. I'll open an issue to track it.

I thought that operator new would inline, but this might depend on which STL implementation is used.

@margnus1
Copy link
Contributor

margnus1 commented Dec 3, 2020

Subsumed by #105

@margnus1 margnus1 closed this as completed Dec 3, 2020
@albertnetymk
Copy link
Author

I'll open an issue to track it.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants