Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive

Conversation

@belka-ew
Copy link
Contributor

@belka-ew belka-ew commented Jul 30, 2018

In rt.arrayassign._d_arraysetassign alloca is called if the buffer should be allocated is larger than 16 bytes:

void[16] buf = void;
void[] tmp;
if (element_size > buf.sizeof)
{
    tmp = alloca(element_size)[0 .. element_size];
}
else
    tmp = buf[];

It is dangerous since alloca unavailable to allocate causes undefined behaviour, so the alloca man page states:

The alloca() function returns a pointer to the beginning of the allocated space. If the allocation causes stack overflow, program behavior is undefined.

See related discussion:
D-Programming-GDC/gdc#699

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @belka-ew! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
19128 enhancement argument to alloca may be too large

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + druntime#2258"

@dlang-bot dlang-bot added the Enhancement New functionality label Jul 30, 2018
void* ptmp;
if (elementSize <= buf.sizeof)
{
ptmp = buf.ptr;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue here that if we are going to fall back to malloc. Then having a pre-allocated buffer is pointless.

This should be:

if (elementSize <= maxAllocaSize)
  ptmp = alloca(elementSize);
else
  ptmp = malloc(elementSize);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or at the very least, this should just be a simple s/alloca/malloc/ swap.

void* ptmp = (elementSize > buf.sizeof) ? malloc(elementSize) : buf.ptr;

// Later...
if (ptmp != buf.ptr)
  free(ptmp);

@JinShil
Copy link
Contributor

JinShil commented Jul 31, 2018

This is an excellent candidate for migration to a template. @belka-ew, how confident are you in modifying DMD to lower assignment expressions to something like void __ArrayAssign(TFrom, TTo)(const TFrom[] from, TTo[] to)? All this size checking could be done at compile-time.

@belka-ew
Copy link
Contributor Author

belka-ew commented Aug 1, 2018

how confident are you in modifying DMD to lower assignment expressions to something like void __ArrayAssign(TFrom, TTo)(const TFrom[] from, TTo[] to)?

At least I could try. This PR has to wait then.

{
ptmp = buf.ptr;
}
else if (elementSize < 1024)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How was 1024 chosen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty random. Should be enough for a lot of data types, and not too much.

@JinShil
Copy link
Contributor

JinShil commented Aug 13, 2018

I recently noticed that DMD's __alloca implementation has a stack overflow check. Can that be utilized in this implementation, and is it consistent with other compiler implementations?

@ibuclaw
Copy link
Member

ibuclaw commented Aug 13, 2018

From memory, no. You can still get a pointer returned from alloca that will crash if you try to dereference.

Try setting the stack size to a low value and call alloca with a number slightly higher than that.

@ibuclaw
Copy link
Member

ibuclaw commented Dec 16, 2018

Moving PR to #2409

@ibuclaw ibuclaw closed this Dec 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Enhancement New functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants