Raised This Month: $32 Target: $400
 8% 

What is a 'segmentation fault?'


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ajr1234
Senior Member
Join Date: Mar 2011
Location: Chicago, IL, U.S.A.
Old 03-26-2011 , 19:09   What is a 'segmentation fault?'
Reply With Quote #1

I made my own extension but when I load it, the server crashes with a segmentation fault. What is that? Thanks for your help!
ajr1234 is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 03-26-2011 , 23:20   Re: What is a 'segmentation fault?'
Reply With Quote #2

I'll let others explain the technical part, but it's obviously a error related to memory/pointers.

I'm not a C++ programmer, but you should check that your pointers/handles/etc. are valid. Maybe it's a simple typo somewhere.

Getting the stack trace would be the first step to figure out where this happened.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)
rhelgeby is offline
Send a message via MSN to rhelgeby
RedSword
SourceMod Plugin Approver
Join Date: Mar 2006
Location: Quebec, Canada
Old 03-27-2011 , 04:24   Re: What is a 'segmentation fault?'
Reply With Quote #3

http://en.wikipedia.org/wiki/Segmentation_fault

A segmentation fault can happens when you're trying to read/write (generally more write) memory you're not suposed to be able to. It's kinda "memory access violation" in Windows.

This can happen when, let's say for example, you're getting a pointer value and modify it (so you get out of bound of your program's memory range) and poof ! Segfault.

Hope that makes thing clearer =x
RedSword is offline
ajr1234
Senior Member
Join Date: Mar 2011
Location: Chicago, IL, U.S.A.
Old 03-27-2011 , 13:55   Re: What is a 'segmentation fault?'
Reply With Quote #4

Thank you all for your help. Can this also happen because of a detoured function? I found a function I wanted to block in server.so with Ida, and then I used CDetour and a detour template (found in an example somewhere) and blocked the function that way. Or is that the hard way to block a function?
ajr1234 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-27-2011 , 14:07   Re: What is a 'segmentation fault?'
Reply With Quote #5

You are probably using CDetour incorrectly, could you paste your code and the prototype of the function you are trying to detour?
__________________
asherkin is offline
ajr1234
Senior Member
Join Date: Mar 2011
Location: Chicago, IL, U.S.A.
Old 03-27-2011 , 20:52   Re: What is a 'segmentation fault?'
Reply With Quote #6

At first, I couldn't even get it to compile, but with help from another coder, I ended up with the following code This is the function I'm trying to detour:
Code:
namespace Detours {

class CBasePropDoor;

typedef bool (CBasePropDoor::*DoorCanOpenFunc)();

class CBasePropDoor : public DetourTemplate<DoorCanOpenFunc, CBasePropDoor>
{
private: //note: implementation of DetourTemplate abstracts

	bool DoorCanOpen();

	// get the signature name from the game conf
	virtual const char *GetSignatureName()
	{
		return "CBasePropDoor_DoorCanOpen";
	}

	//notify our patch system which function should be used as the detour
	virtual DoorCanOpenFunc GetDetour()
	{
		return &CBasePropDoor::DoorCanOpen;
	}
};

}
extension.cpp:
Code:
Sample g_Sample;		/**< Global singleton for extension's main interface */

SMEXT_LINK(&g_Sample);

IForward *g_pFwdDoorCanOpen = NULL;
PatchManager g_PatchManager;
IGameConfig *g_pGameConf = NULL;

bool Sample::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
    char conf_error[250];
    gameconfs->LoadGameConfigFile(GAMECONFIG_FILE, &g_pGameConf, conf_error, sizeof(conf_error));
    g_pFwdDoorCanOpen = forwards->CreateForward("L4D_DoorCanOpen", ET_Event, 2, /*types*/NULL, Param_Array, Param_Array);
}

void Sample::SDK_OnAllLoaded()
{
	g_PatchManager.Register(new AutoPatch<Detours::CBasePropDoor>());
}

void Sample::SDK_OnUnload()
{
    g_PatchManager.UnregisterAll();
	forwards->ReleaseForward(g_pFwdDoorCanOpen);
}

namespace Detours
{
        int iDoor = 0;

	bool CBasePropDoor::DoorCanOpen()
	{
		cell_t result = Pl_Continue;

                if (iDoor != 0)
                    return false;

                iDoor++;

                return true;
	}
}
And the gamedata:
Code:
"Games"
{
	"left4dead2"
	{
		"Offsets"
		{
			/* Offset into CDirectorMusicBanks::OnRoundStart */
			"TheDirector"
			{
				"windows"	"8"
			}
		}
		
		"Signatures"
		{
			"CBasePropDoor_DoorCanOpen"
			{
					"library"    "server"
					"linux"  "@_ZN13CBasePropDoor11DoorCanOpenEv"
			}

			/* Find the Director/ZombieManager singleton classes */
			
			"TheDirector"
			{
				"library"	"server"
				"linux"	"@TheDirector"
			}
		}
	}
}
As you can see, I clearly don't know what I'm doing.
ajr1234 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-28-2011 , 00:40   Re: What is a 'segmentation fault?'
Reply With Quote #7

That template system looks extremely convoluted.

Try just using CDetour as-is, my ZombieFix extension (linked in my sig) is a simple example of just blocking a function.
__________________
asherkin is offline
ajr1234
Senior Member
Join Date: Mar 2011
Location: Chicago, IL, U.S.A.
Old 03-28-2011 , 19:36   Re: What is a 'segmentation fault?'
Reply With Quote #8

Awesome That's much more clear than what I was trying to do!

Can you explain the arguments? I don't know what to put in because I don't know what the arguments mean. -_-
Code:
DETOUR_DECL_MEMBER2(DoorCanOpen, void, bool, door, bool, door)
Edit: nvm, since the function I was trying to detour had no arguments and just a return type of bool, I used DETOUR_DECL_MEMBER0. I took your Zombie Fix code and changed it slightly so it would compile. It loads in SM, no problems. But it does nothing. It doesn't report anything to the server or anything -_- Is there PrintToChatAll() or something similar via an extension?

Code:
	detourDoorCanOpen = DETOUR_CREATE_MEMBER(DoorCanOpen, "DoorCanOpen");

	if (detourDoorCanOpen != NULL)
	{
		detourDoorCanOpen->EnableDetour();
		snprintf(error, maxlength, "Detoured!!!");
	}
	else
	{
	    snprintf(error, maxlength, "[Open Door] COULD NOT DETOUR!!!");
		return false;
	}

	snprintf(error, maxlength, "Extension loaded!");

	return true;

Last edited by ajr1234; 03-28-2011 at 22:34.
ajr1234 is offline
Sweet Blasphemy
BANNED
Join Date: Apr 2011
Old 04-06-2011 , 22:40   Re: What is a 'segmentation fault?'
Reply With Quote #9

You should use
Code:
g_pSM->LogMessage(myself, "blahblah");
to log messages. The error argument is only used when you return false, I presume.
Sweet Blasphemy is offline
ajr1234
Senior Member
Join Date: Mar 2011
Location: Chicago, IL, U.S.A.
Old 04-08-2011 , 18:08   Re: What is a 'segmentation fault?'
Reply With Quote #10

I see, thanks. Last question: is there a way to print to the chat box, like in plugins where you can just use PrintToChatAll() and have a chat message sent. ie. "[EXT] Door cannot be opened!" in the chat box?
ajr1234 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode