Skip to content

Allocate space for user stack at link time - #179

Merged
dschuff merged 1 commit into
masterfrom
allocate_stack
Feb 4, 2016
Merged

dschuff merged 1 commit into
masterfrom
allocate_stack

Conversation

@dschuff

@dschuff dschuff commented Feb 3, 2016

Copy link
Copy Markdown
Member

Currently s2wasm pre-allocates space for the special __stack_pointer
variable but not for the actual stack, and __stack_pointer is null. On
real systems the stack is allocated by the dynamic linker, thread
runtime, or other toolchain/system-provided startup code. Since we don't
have any of that in wasm yet, just allocate the stack in the linker for now.

Comment thread src/s2wasm.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, let's make this a flag.

@dschuff

dschuff commented Feb 4, 2016

Copy link
Copy Markdown
Member Author

OK, addressed the comments. please take another look at the code. I'm still looking at how to update the tests. Are the test cases in dot_s autogenerated, or manual?

@kripken

kripken commented Feb 4, 2016

Copy link
Copy Markdown
Member

Manual. But you can update their outputs automatically with auto_update_tests.py.

Comment thread src/s2wasm.h Outdated
prepare();
// Place the stack pointer at the bottom of the linear memory, to keep its
// address small (and thus with a small encoding).
place_stack_pointer(stackSize);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

placeStackPointer for consistency, please

@kripken

kripken commented Feb 4, 2016

Copy link
Copy Markdown
Member

I had some more general thoughts about this.

First, since the stack comes after the static allocations, all the size does for us is potentially increase memory. But the program will need more memory anyhow for sbrk/malloc. And probably the memory size should be arriving from the LLVM backend anyhow? @sunfishcode?

Overall, the program's runtime/startup code might do the following:

  1. Receive info on where static allocations end (staticBump in s2wasm the metadata, the driver can forward it).
  2. Receive the location of the stack pointer (also in the metadata).
  3. Instantiate the wasm module.
  4. Decide where to place the stack, what size to make it, etc. This might be done at runtime, and fits in with making everything as relocatable as possible, which is an idea @jfbastien and I were talking about. It's also how Emscripten does this now.
  5. Write the stack pointer's initial value, chosen in 4, into the stack pointer's location in memory.
  6. It can also set up the rest of what it needs for memory, like sbrk can be right after the stack, etc.

This seems more flexible, does less work in s2wasm, and is consistent with Emscripten.

Thoughts?

@dschuff

dschuff commented Feb 4, 2016

Copy link
Copy Markdown
Member Author

Mostly I want this to be able to test LLVM independently of all the emscripten machinery, it doesn't have to be the only or final mechanism. But, here are some other thoughts too:

First, of course the stack increases memory. Most programs will still need it. And there's no good way to resize it without mmap (or having it grow upward into a space that can be increased by increasing the entire linear memory). So it will have to be large enough, according to the program's needs.

The program will also need heap memory; it would be the job of the allocator to allocate that, and how much it would want initially would be a design decision; presumably it would need to balance the cost of having a large initial size against the (probably large) cost of having to call grow_memory. It doesn't necessarily need to be aware of the stack, although it could.

Also what do you mean by startup code?
If, for the statically-linked case, you mean wasm code called before main(), then yes that's kind of what I had in mind eventually, but we don't have a mechanism for that yet. nor any common libc startup code (and it would still be nice to be able to test single files without that).

The LLVM backend doesn't really have a good idea how much stack would be needed (other than whether it's needed at all), but it is possible to have all the stack info come from the .s file. Previously I was testing locally by appending (or prepending) the following to the .s file:

        .globl  .stack
        .type   .stack,@object
.stack:
        .zero   4096
        .size   .stack, 4096

        .type   __stack_pointer,@object
__stack_pointer:
        .int32  .stack+4096

If we made s2wasm support commons then it would even support linking multiple files.
I'm fine with making things more dynamic (this is just for a start, for testing things), although I'd rather avoid having things outside the wasm module completely (e.g. in JS startup code).

@kripken

kripken commented Feb 4, 2016

Copy link
Copy Markdown
Member

Oh, are you just looking for something temporary to replace that appended code? I'm fine with something for that. But I'm a little worried this will help here but then need to be replaced later with something better, and tests will break during that change, etc.

In any case, I think we should be able to support both a simple fixed stack if that's useful for you, and the flexible runtime approach that Emscripten uses. Perhaps the option here could do nothing (except return the location of the stack pointer in metadata) when not set? Then the flexible approach would do that.

Mostly I want this to be able to test LLVM independently of all the emscripten machinery, it doesn't have to be the only or final mechanism.

Sure. I was saying that I think it makes sense to be consistent with what Emscripten does here, both because the consistency is nice for itself, and also because what Emscripten does is the flexible approach of letting the runtime set this, which should be easy for other things to use as well.

Also what do you mean by startup code?

I meant the outside JS code. The code that loads the wasm module, connects it to the rest of the world, etc.

@jfbastien

Copy link
Copy Markdown
Member

I think it would be good to have a temporary thing for now, maybe with a command-line parameter to s2wasm allowing us to specify a non-default stack size.

In the long term have the toolchain-provided _start invoke setrlimit(RLIMIT_STACK, &rl); seems like the right approach. A program would have zero stack initially, and _start would set things up.

jfbastien added a commit that referenced this pull request Feb 4, 2016
As spec'd in: WebAssembly/design#495
And discussed in: WebAssembly/spec#231

This will make it simpler and more uniform to add a start entry point.

s2wasm is the right place to add start because it'll eventually need to
do other basic setup, e.g. put code in start to setup the stack, as
dschuff is doing in:
  #179
Or rather, the linker is the right place and s2wasm happens to act as
our linker right now.
@jfbastien jfbastien mentioned this pull request Feb 4, 2016
@dschuff

dschuff commented Feb 4, 2016

Copy link
Copy Markdown
Member Author

Right, in other words this isn't a toolchain design proposal, it's just supposed to be part of the minimal test harness that we're already using for the torture tests, where we don't need a libc or multi-file linking or startup code.
Maybe we should make the option default to 0 and not create a stack if not set.

More broadly, I do think we want a more flexible mechanism than this; something that works for now, and when we get mmap, and when we get dynamic linking. I think in general I would prefer the stack allocation to be handled inside the wasm module if possible but I'm open to discussion on that, and this probably isn't the place.

Anyway I'll make the option more optional.

@dschuff

dschuff commented Feb 4, 2016

Copy link
Copy Markdown
Member Author

Updated, rebased, and squashed.

@kripken

kripken commented Feb 4, 2016

Copy link
Copy Markdown
Member
  1. I think we still need to create the stack pointer, even with this option off, as we do before this pull. The only thing I think this pull's option should add is to create a segment in memory for the stack and to bump the initial and max memory sizes.
  2. Given that, how about calling this option "create stack"? Or "allocate stack"?

As a followup, I think we'll also want a "set memory sizes" option to set the initial and max memory sizes, if they aren't arriving from the LLVM backend. I can do that.

@dschuff

dschuff commented Feb 4, 2016

Copy link
Copy Markdown
Member Author

Yeah, I was just thinking about that. I'm fine with keeping that as-is for now.

In general though I actually think maybe we don't need to create the stack pointer if there are no references to it in the file. Since we are assuming that this is an already-linked module, if there are no references to the stack pointer, then there need be no stack pointer and no stack.
So there are 3 possibilities:

  1. s2wasm should create a stack (specified explicitly by an option), in which case there needs to be a stack pointer.
  2. There are no uses of the stack pointer, in which case there need not be a stack pointer or a stack.
  3. There are uses of the stack pointer but the stack will be allocated by some startup code (i.e. dynamically). This is presumably what we'd use in nontrivial programs in the long run.

For case 3, there are a couple of options.
a) s2wasm could determine whether there are any references to the stack pointer and conditionally allocate it. It would be easy to put it at the end after the user variables, but we might prefer to have it before the user variables to keep its address small (which would be slightly less trivial in s2wasm but maybe not so bad).
b) LLVM could keep track of whether a stack pointer is needed and emit its own .globl (or maybe .common in the future if we want to link .s or ELF files) declaration for the stack pointer. In this case s2wasm wouldn't even need to have any special-casing for the stack pointer at all (although it would still be needed if we wanted to expose the stack pointer address to JS, and it could have special-casing for that purpose).

@dschuff

dschuff commented Feb 4, 2016

Copy link
Copy Markdown
Member Author

Updated the branch to always generate the pointer as before.

@kripken

kripken commented Feb 4, 2016

Copy link
Copy Markdown
Member

Hmm, I don't see much harm in always creating the stack pointer itself. Since it's tiny. And trying to optimize it out means the code will never have a stack, but we might need one in linked code (JS today, or other wasm some day).

Comment thread src/s2wasm-main.cpp Outdated
[](Options *o, const std::string &argument) {
o->extra["global-base"] = argument;
})
.add("--stack-size", "-s", "Size of the user stack in linear memory",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

as mentioned earlier, the name seems wrong to me, if (1) we create the stack pointer anyhow, and (2) the stack might be created by a runtime later. Instead, how about --create-stack or --create-stack-segment? The last seems most precise, but maybe long.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or --create-stack-storage?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or --allocate-stack?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sorry I missed the comment about the name. How about --allocate-stack?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Heh, I think we just wrote it at the same time ;) Sounds great!

@dschuff

dschuff commented Feb 4, 2016

Copy link
Copy Markdown
Member Author

WRT creating the stack pointer at all: That's why the dynamic linker would set up the stack in the dynamic linking case. Otherwise if there are no references and we are statically linked, then there can be nothing that uses the stack pointer. (This includes JS, which shouldn't be touching the user stack anyway).

@kripken

kripken commented Feb 4, 2016

Copy link
Copy Markdown
Member

I see your point, yeah.

@dschuff

dschuff commented Feb 4, 2016

Copy link
Copy Markdown
Member Author

OK, --allocate-stack it is.

Comment thread src/s2wasm-main.cpp Outdated
size_t globalBase = options.extra.find("global-base") != options.extra.end()
? std::stoull(options.extra["global-base"])
: 1;
size_t stackSize =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please rename this too

@kripken

kripken commented Feb 4, 2016

Copy link
Copy Markdown
Member

lgtm with that last variable renamed, and squashed.

Currently s2wasm pre-allocates space for the special __stack_pointer
variable but not for the actual stack, and __stack_pointer is null. On
real systems the stack is allocated by the dynamic linker, thread
runtime, or other toolchain/system-provided startup code. Since we don't
have any of that in wasm yet, just add an option to allocate the stack
in the linker for now, so we can continue to run single-file libc-free tests.
dschuff added a commit that referenced this pull request Feb 4, 2016
Allocate space for user stack at link time
@dschuff
dschuff merged commit 9b12bb3 into master Feb 4, 2016
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

Successfully merging this pull request may close these issues.

3 participants