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

When put on stack or within struct, dramsim3::MemorySystem corrupts memory #47

Open
CircuitCoder opened this issue Dec 23, 2023 · 0 comments

Comments

@CircuitCoder
Copy link

Because the class layout given in dramsim3.h and memory_system.h are different (the one given in public interface has no private members, which affects memory layout), when dramsim3::MemorySystem are placed on stack or within other containers, it may corrupts what's placed directly after it.

Notice that in memory_system.h there are two fields, but in dramsim3.h there is not. Is there a reason for this difference?

The problem I met is with std::optional<T>. Its implementation in g++(libstdc++) is basically as followed:

template<typename T>
class optional<T> {
  union {
    struct {} _empty;
    T _data;
  }
  bool engaged;
};

If T's apparent size is smaller than it's real size, then the mutation within T will affect engaged.

@CircuitCoder CircuitCoder changed the title When put on stack or struct, dramsim3::MemorySystem corrupts memory When put on stack or within struct, dramsim3::MemorySystem corrupts memory Dec 23, 2023
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

1 participant