-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
How to initialize STACKTOP? #5771
Comments
Is this a wasm side module? If so, then it's a dynamic library, and has a |
Actually I don't see stack allocation there, so the loader assumes the stack is handled internally by the module. But you can do it either way, as long as you coordinate. |
That's helpful, thank you! I will try assess this approach and return back |
I realized that Is there a way to find out the |
Oh, yes, if you don't need it to be relocatable then maybe not In normal mode, you can see it in the generated JS, in the form
where X is a number. It's emitted from emscripten.py, which receives it as metadata from the backend. |
My experiments and research led me to conclude that I need something like |
Related, I'm trying to build a common js environment (with future interest in non-JS as well) for any emcc-generated executable. One of the issues I've hit is that "+ X". Is there some way to get it from the wasm module itself? |
In a wasm module with a dynamic linking section, there is info for that, https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md I believe also in the object file linking docs, https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md . But a normal wasm will not contain it, a toolchain needs to create a convention for it. |
Thanks for the info. I want it in normal wasm. Could emscripten make a convention, such as a standard exported symbol? |
The wasm tool conventions docs mentioned above decided to add new wasm custom sections, for object and library files respectively. Emscripten supports the library one already, and will support object files soon I think (with the llvm wasm backend). |
Would it be wiser for me then to wait on the upcoming features before continuing my project? |
I don't fully understand your project. Can you explain more in detail what you're doing, what the goals are, etc.? |
Here's a project with braindump and some exported code from emsdk: https://github.com/tjpalmer/dae I don't know for sure what I can and can't accomplish with my time constraints, but even separate from any lofty goals, it seems like the ability to run wasm executables from a preset environment has value. (And a different use case from building a specially tailored page/site/app, which is where emsdk is focused now, but still a valuable use case, in my opinion.) |
Actually, it occurs to me that I have a workaround. I probably should have an app descriptor for my purposes, anyway, and I can throw metadata in there, including any atinit function names that I know have been exported from the module. But again, separate from anything I'm doing, I think it would be good for both wasm and emscripten to support self-contained executables, given some predefined environment. |
Sorry for the spam, but if you follow my above link, I now have proof of concept demos that use a small descriptor (parsed from the emsdk-compiled js) that extracts the static bump and other key values into a small descriptor. I'll go with this workaround for my own needs for now, but it would be great not to have to parse the js file. Some exported metadata file from emsdk might be one option until standards exist. |
Interesting project. I don't think I fully understand it all, but I do get that you want a constrained/secure environment - and I guess knowing the app details are necessary for that. Yeah, parsing the JS makes sense for now. We could also consider adding an option to export that metadata - static bump, list of global ctors, maybe other stuff I'm forgetting. Maybe like |
That would be great if you get a chance. If I make progress on my project, my current short list might get longer based on the info I find I need to extract. Thanks for all the feedback. |
Just my 2 cents on why i'm interested in this. I'm working on a project integrating WebAssembly into a blockchain. Before execution of contract starts, we need to initialize execution enviroment, including to setup stack for the contract. At the moment, we just hardcode the STACK_TOP to an arbitrary high-enough address. But I believe that contracts should be self-contained and initialize enviroment for themselves (So because of it I think In a real blockchain, users that want to execute some code actually pays for every instruction that was executed by a contract and for every resource it touched (i.e memory or storage). |
@tjpalmer @kripken I've also just recently ran into this issue when analyzing emscripten-generated WASM modules. Is it possible to make |
@rianhunter Sounds good, I think it makes sense to add an option to export STATIC_BUMP (and maybe also a few others, like GLOBAL_BASE, which is where globals are allocated - in practice, the start of static memory). Let me know if you run into any issues implementing it. One question is where to do it - implementing it in asm2wasm versus the llvm wasm backend would be a little different (in asm2wasm it would be in emscripten.py, in the llvm wasm backend probably in wasm-emscripten-finalize in binaryen). Probably not much work in either, though. |
@kripken Probably best to implement it in both, right? The impression I get is that the llvm wasm backend is the future yet still not the default. When it comes to modeling the protocol between the runtime and the module, I see |
Yeah, exactly. Doing in both is best, but if you can only do one, I'd do the llvm wasm backend.
I think that's right - we have setErrNo in JS so that the JS filesystem code can call it. And that updates errno in the compiled C code using errno_location so that C code just looking at
I think that's right too. At runtime in JS we calculate the location for the stack, and then provide that to the module. It might be nice eventually to have an option for fixing the location of the stack at compile time. There was some discussion about it on the mailing list, regarding the ctor evaller. |
Okay, I hacked together a workaround on my project for now, and furiously working there, but I will return to this! |
This is a major issue in |
See #6457 (comment) for the bigger picture here. But that should not stop someone from adding metadata for STACK_BUMP etc. in the meantime. |
See #7815 for pending fix |
Thanks @rianhunter, closing. |
I'm working in an non-js environment.
So before an execution of the wasm module I need to provide initial
STACKTOP
.But the problem is
STACKTOP
depends onSTATICTOP
, i.e. should be just after static data (+ alignment). AFAIK, this value is only available in JS runtime.I'm not sure, how should I initialize
STACKTOP
?The text was updated successfully, but these errors were encountered: