Skip to content

[Stdlib] Fix entry-point-based process args #3108

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

Merged
merged 1 commit into from
Jul 14, 2016

Conversation

CodaFi
Copy link
Contributor

@CodaFi CodaFi commented Jun 21, 2016

What's in this pull request?

Provides a new fallback for Process arguments for those instances where we do not own main (e.g. Frameworks, Objective-C owns main.m or main.c, etc.). This includes a number of platform-specific specializations of a new runtime function _swift_get_args and a new thread-safe interface to Process.unsafeArgv.

Resolved bug number: (SR-1119)


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

@CodaFi CodaFi changed the title Fix entry-point-based process args [Stdlib] Fix entry-point-based process args Jun 21, 2016
size_t size = 0;
std::vector<char *> argvec;
while(getdelim(&arg, &size, 0, cmdline) != -1) {
argvec.push_back(arg);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need to reset arg and size here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both of those are purely out parameters and I can't find any precedence for resetting them.

Copy link
Contributor

@gribozavr gribozavr Jun 21, 2016

Choose a reason for hiding this comment

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

From the man page:

Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc(3)-allocated buffer *n bytes in size.

That is, subsequent calls would overwrite what the first call allocated.

@CodaFi CodaFi force-pushed the argumental branch 2 times, most recently from a2d3ca1 to b81002b Compare June 21, 2016 21:55
FILE *cmdline = fopen("/proc/self/cmdline", "rb");
if (!cmdline) {
swift::fatalError(0,
"fatal error: Unable to open command line interface.\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think using /proc/self/cmdline in the error will help a lot of people who run Swift in restricted environments. Seeing a message about /proc makes it immediately obvious what's wrong (/proc not available, or not mounted etc.)


let newArgs = _unsafeArgv ?? _swift_stdlib_getUnsafeArgcArgv(&_argc)
if _stdlib_atomicCompareExchangeStrongPtr(
object: argumentsPtr, expected: &_swift_stdlib_ProcessUnsafeArgv, desired: newArgs) {
Copy link
Contributor

@gribozavr gribozavr Jun 21, 2016

Choose a reason for hiding this comment

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

Expected should be nil. We are initializing _swift_stdlib_ProcessUnsafeArgv from nil to newArgs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. Accidentally committed while in the middle of refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like we should be able to get rid of _Box and the ARC mess altogether, by doing something like this:

static var unsafeArgv = _unsafeArgvFromMain ?? _unsafeArgvFromRuntime
static var _unsafeArgvFromRuntime = _swift_stdlib_getUnsafeArgcArgv(&_argc)

I guess that's not 100% thread-safe because _unsafeArgvFromMain could be set from main while being read on another thread spawned prior to entering main, but something like this could work, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

There's not ARC or boxes now, everything is just pointers.

@jrose-apple
Copy link
Contributor

I think we should also stop using main to set argc/argv except in interpreter mode, but we can do that in a follow-up commit.

@CodaFi CodaFi force-pushed the argumental branch 3 times, most recently from d5e60bd to c0cbd40 Compare June 24, 2016 00:16
}

@_versioned
/// The backing static variable for argument count may come either from the
/// entry point or it may need to be computed e.g. if we're in the REPL.
Copy link
Contributor

Choose a reason for hiding this comment

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

The doc comment should be above the attribute.

@CodaFi
Copy link
Contributor Author

CodaFi commented Jul 11, 2016

I spoke with @trentxintong earlier today and he does not believe that the old static initializer problems will manifest themselves here. I've folded everything into static lets.

@CodaFi CodaFi force-pushed the argumental branch 3 times, most recently from f0ad629 to 4314cd0 Compare July 11, 2016 23:30
@CodaFi
Copy link
Contributor Author

CodaFi commented Jul 11, 2016

@swift-ci please test.

@@ -34,7 +34,9 @@ namespace swift {

namespace immediate {

bool loadSwiftRuntime(StringRef runtimeLibPath);
// Returns a handle to the runtime suitable for other 'dlsym' or 'dlclose'
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: please make this a proper doc comment, so that it shows up in Quick Help.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll get it when I remove the compiler intrinsic.

@jrose-apple
Copy link
Contributor

Happy to be getting this nailed down!

@CodaFi
Copy link
Contributor Author

CodaFi commented Jul 12, 2016

@swift-ci please test.

@CodaFi
Copy link
Contributor Author

CodaFi commented Jul 12, 2016

The failures look unrelated, but I want a clean merge.

@CodaFi CodaFi force-pushed the argumental branch 8 times, most recently from d18a092 to b31b630 Compare July 14, 2016 01:24
Provides a new fallback for Process arguments for those instances where we do
not own main (e.g. Frameworks, Objective-C owns main.m or main.c, etc.). This
includes a number of platform-specific specializations of argument grabbing
logic and a new thread-safe interface to Process.unsafeArgv.

main()  | _NSGetArgc/_NSGetArgv    | /proc/self/cmdline     | __argc/__argv
--------|--------------------------|------------------------|---------------
Scripts | OS X, iOS, tvOS, watchOS | Linux, FreeBSD, Cygwin | Windows

For interpreted Swift where we must filter out the arguments we now do so by
loading the standard library and calling into new SPI to override the arguments
that would have been grabbed by the runtime. This implementation completely
subsumes the use of the entry point  '_stdlib_didEnterMain' and it will be
removed in a future commit.
@CodaFi
Copy link
Contributor Author

CodaFi commented Jul 14, 2016

@swift-ci please test and merge.

@CodaFi
Copy link
Contributor Author

CodaFi commented Jul 14, 2016

Finally! The build failures aren't my fault.

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.

4 participants