Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 03, 2010 segfaults | ||||
---|---|---|---|---|
| ||||
Hello. I'm trying to invoke a command inside d, and it returns a success code when the command in question segfaults. any ideas? // the caller import std.process; int main(){ auto r = system("./test"); return(r); } //test.d import std.stdio; void main() { Object o; writeln(o.toString()); } |
May 03, 2010 Re: segfaults | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On Mon, 03 May 2010 15:54:28 -0500, Ellery Newcomer wrote:
> Hello.
>
> I'm trying to invoke a command inside d, and it returns a success code when the command in question segfaults.
>
> any ideas?
>
> // the caller
> import std.process;
>
> int main(){
> auto r = system("./test");
> return(r);
> }
>
>
> //test.d
> import std.stdio;
>
> void main()
> {
> Object o;
> writeln(o.toString());
> }
It's a null dereference. What you're doing is essentially
Object o = null;
writeln(o.toString());
-Lars
|
May 03, 2010 Re: segfaults | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars T. Kyllingstad | On 04/05/10 08:57, Lars T. Kyllingstad wrote:
> On Mon, 03 May 2010 15:54:28 -0500, Ellery Newcomer wrote:
>
>> Hello.
>>
>> I'm trying to invoke a command inside d, and it returns a success code
>> when the command in question segfaults.
>>
>> any ideas?
>>
>> // the caller
>> import std.process;
>>
>> int main(){
>> auto r = system("./test");
>> return(r);
>> }
>>
>>
>> //test.d
>> import std.stdio;
>>
>> void main()
>> {
>> Object o;
>> writeln(o.toString());
>> }
>
> It's a null dereference. What you're doing is essentially
>
> Object o = null;
> writeln(o.toString());
>
> -Lars
I believe his problem is that the return code of the caller indicates success.
|
May 03, 2010 Re: segfaults | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bernard Helyer | On Mon, 03 May 2010 17:25:30 -0400, Bernard Helyer <b.helyer@gmail.com> wrote:
> On 04/05/10 08:57, Lars T. Kyllingstad wrote:
>> On Mon, 03 May 2010 15:54:28 -0500, Ellery Newcomer wrote:
>>
>>> Hello.
>>>
>>> I'm trying to invoke a command inside d, and it returns a success code
>>> when the command in question segfaults.
>>>
>>> any ideas?
>>>
>>> // the caller
>>> import std.process;
>>>
>>> int main(){
>>> auto r = system("./test");
>>> return(r);
>>> }
>>>
>>>
>>> //test.d
>>> import std.stdio;
>>>
>>> void main()
>>> {
>>> Object o;
>>> writeln(o.toString());
>>> }
>>
>> It's a null dereference. What you're doing is essentially
>>
>> Object o = null;
>> writeln(o.toString());
>>
>> -Lars
>
>
> I believe his problem is that the return code of the caller indicates success.
Could it be perhaps that it can't possibly get at that status? Remember, system runs /bin/sh -c, so all you can get as status is the return code of /bin/sh (which didn't segfault).
-Steve
|
May 03, 2010 Re: segfaults | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 04/05/10 09:49, Steven Schveighoffer wrote:
> On Mon, 03 May 2010 17:25:30 -0400, Bernard Helyer <b.helyer@gmail.com>
> wrote:
>>
>> I believe his problem is that the return code of the caller indicates
>> success.
>
> Could it be perhaps that it can't possibly get at that status? Remember,
> system runs /bin/sh -c, so all you can get as status is the return code
> of /bin/sh (which didn't segfault).
>
> -Steve
sh -c returns failure if the specified executable segfaults.
|
May 03, 2010 Re: segfaults | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 05/03/2010 04:49 PM, Steven Schveighoffer wrote:
>
> Could it be perhaps that it can't possibly get at that status? Remember,
> system runs /bin/sh -c, so all you can get as status is the return code
> of /bin/sh (which didn't segfault).
>
> -Steve
All I know is the analogous code in python returns the expected 139
|
May 03, 2010 Re: segfaults | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On Mon, 03 May 2010 17:34:51 -0500, Ellery Newcomer wrote:
> On 05/03/2010 04:49 PM, Steven Schveighoffer wrote:
>>
>> Could it be perhaps that it can't possibly get at that status? Remember, system runs /bin/sh -c, so all you can get as status is the return code of /bin/sh (which didn't segfault).
>>
>> -Steve
>
> All I know is the analogous code in python returns the expected 139
What OS are you running on? In D2, this the definition of system():
int system(string command)
{
if (!command) return std.c.process.system (null);
const commandz = toStringz (command);
invariant status = std.c.process.system (commandz);
if (status == -1) return status;
version (Posix)
return (status & 0x0000ff00) >>> 8;
else
return status;
}
And "(139 & 0x0000ff00) >>> 8" evaluates to 0. I am not sure why it's
not simply returning the raw status-code, though, and only on Posix
systems -- it must be a Posix-ism I'm not familiar with.
Graham
|
May 04, 2010 Re: segfaults | ||||
---|---|---|---|---|
| ||||
Posted in reply to Graham Fawcett | On 05/03/2010 06:08 PM, Graham Fawcett wrote:
>
> What OS are you running on? In D2, this the definition of system():
>
> int system(string command)
> {
> if (!command) return std.c.process.system (null);
> const commandz = toStringz (command);
> invariant status = std.c.process.system (commandz);
> if (status == -1) return status;
> version (Posix)
> return (status& 0x0000ff00)>>> 8;
> else
> return status;
> }
>
>
> And "(139& 0x0000ff00)>>> 8" evaluates to 0. I am not sure why it's
> not simply returning the raw status-code, though, and only on Posix
> systems -- it must be a Posix-ism I'm not familiar with.
>
> Graham
I'm on fedora, and yeah, I guess it's a posixism. investigation yields
lower 16 bits is coredump flag and signal code
upper 16 bits is status code
I tell ya, working with D, ya learn something new every day [while implementing crap that the provided libraries should handle on their own...]
|
May 04, 2010 Re: segfaults | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bernard Helyer | On Tue, 04 May 2010 09:25:30 +1200, Bernard Helyer wrote:
> On 04/05/10 08:57, Lars T. Kyllingstad wrote:
>> On Mon, 03 May 2010 15:54:28 -0500, Ellery Newcomer wrote:
>>
>>> Hello.
>>>
>>> I'm trying to invoke a command inside d, and it returns a success code when the command in question segfaults.
>>>
>>> any ideas?
>>>
>>> [...]
>>
>> It's a null dereference.
>>
>> [...]
>
> I believe his problem is that the return code of the caller indicates success.
Note to self: Read posts *thoroughly* before answering. Man, I was feeling so pleased with myself for having spotted the error so quickly. ;)
-Lars
|
May 04, 2010 Re: segfaults | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On Mon, 03 May 2010 20:32:03 -0500, Ellery Newcomer wrote: > On 05/03/2010 06:08 PM, Graham Fawcett wrote: >> >> [...] >> >> And "(139& 0x0000ff00)>>> 8" evaluates to 0. I am not sure why it's not simply returning the raw status-code, though, and only on Posix systems -- it must be a Posix-ism I'm not familiar with. >> >> Graham > > I'm on fedora, and yeah, I guess it's a posixism. investigation yields lower 16 bits is coredump flag and signal code upper 16 bits is status code That hardcoded bit shift in std.process is really ugly, and hides what is going on. Here's the correct way to analyse POSIX status codes: import core.sys.posix.sys.wait; ... if (WIFEXITED(status)) writeln("Process exited with code ", WEXITSTATUS(status)); else if (WIFSIGNALED(status)) writeln("Process terminated by signal ", WTERMSIG(status)); In your case the segfault would cause SIGSEGV (signal 11) to be sent to the process, and the the above test would print "Process terminated by signal 11". See "man wait" for more info. > I tell ya, working with D, ya learn something new every day [while implementing crap that the provided libraries should handle on their own...] std.process is currently undergoing a complete redesign, so the current situation should improve in the near future. :) -Lars |
Copyright © 1999-2021 by the D Language Foundation