Source-Python-Dev-Team / Source.Python

This plugin aims to use boost::python and create an easily accessible wrapper around the Source Engine API for scripter use.
http://forums.sourcepython.com
GNU General Public License v3.0
163 stars 31 forks source link

CS:S - Player.drop_weapon crash #373

Closed Frag1337 closed 3 years ago

Frag1337 commented 3 years ago

Hello,

when using following snippet while being on a team/alive on the server:

from filters.players import PlayerIter

for player in PlayerIter():
    print('Trying to remove weapons from {}..'.format(player.name))

    for weapon in player.weapons():
        print('Found {} from {}. Lets drop it.'.format(weapon.classname, player.name))

        player.drop_weapon(weapon)

        print('Aaand remove it..')

        weapon.remove()

I get following crash:

sp plugin load test
[SP] Loading plugin 'test'...
Trying to remove weapons from Frag..
Found weapon_knife from Frag. Lets drop it.
Segmentation fault (core dumped)
Add "-debug" to the ./srcds_run command line to generate a debug.log to help with solving this problem
Mon Dec 14 19:40:46 UTC 2020: Server restart in 10 seconds
sp info

IMPORTANT: Please copy the full output.
--------------------------------------------------------
Checksum      : 17af3a55546e8fed9c8e9b322e817b97
Date          : 2020-12-14 19:43:07.359723
OS            : Linux-5.4.0-58-generic-x86_64-with-debian-bullseye-sid
Game          : css
SP version    : 702
Github commit : 99ed4dca7b439f5fc4ade054355767b23af497df
Server plugins:
   00: Source.Python, (C) 2012-2020, Source.Python Team.
SP plugins:
--------------------------------------------------------

The server also crashes on

weapon.remove()

by uncommenting player.drop_weapon(weapon)

sp plugin load test
[SP] Loading plugin 'test'...
Trying to remove weapons from Frag..
Found weapon_knife from Frag. Lets drop it.
Aaand remove it..
Segmentation fault (core dumped)
Add "-debug" to the ./srcds_run command line to generate a debug.log to help with solving this problem
Mon Dec 14 19:45:04 UTC 2020: Server restart in 10 seconds

Not sure if its helpful, but I've used this fix to make Source.Python work:

https://forums.sourcepython.com/viewtopic.php?p=13976#p13976 Method 2

CookStar commented 3 years ago

What happens when you install libffi6 using this method? Of course, don't forget to remove the symbolic link to libffi7.

Frag1337 commented 3 years ago

What happens when you install libffi6 using this method? Of course, don't forget to remove the symbolic link to libffi7.

I removed the symlink and used that method but it gave me an error with the package not being compitable.

So I downloaded the file manually, dropped it into /home/user/ and ran it with sudo apt install ./libffi6_3.2.1-9_i386.deb successfully. Sadly the same results from top.

Can someone replicate this? Is it a wrong offset or something different? I sadly have no idea about solving crashes :/

In another issue (https://github.com/Source-Python-Dev-Team/Source.Python/issues/334#issuecomment-744646437) I mentioned that I've reinstalled 2x VMs on my Windows 10 PC with Debian and Ubuntu and both crashed when using this snippet aswell. With Debian not requiring a workaround for libffi6, but sadly resulted in the same crash.

CookStar commented 3 years ago

What happens if you try this code? Change player.drop_weapon(weapon) to drop_weapon_test(player, weapon).

def drop_weapon_test(player, weapon):
    import ctypes
    from memory.hooks import set_hooks_disabled
    functype = ctypes.CFUNCTYPE(None, *(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p))
    drop_weapon = functype(player.drop_weapon.address)
    player_p = ctypes.c_void_p(player.pointer.address)
    weapon_p = ctypes.c_void_p(weapon.pointer.address)
    set_hooks_disabled(True)
    print("Drop the weapon by ctypes.")
    drop_weapon(player_p, weapon_p, None, None)
    print("Success.")
    set_hooks_disabled(False)

    player.equip_weapon(weapon)
    set_hooks_disabled(True)
    print("Drop the weapon.")
    player.drop_weapon(player, weapon)
    print("Success.")
    set_hooks_disabled(False)
Frag1337 commented 3 years ago

What happens if you try this code?

sp plugin load test
[SP] Loading plugin 'test'...
1.1 Trying to remove weapons from Frag..
1.1 Found weapon_knife from Frag. Lets drop it.
2.1 Drop the weapon by ctypes.
2.2 Success.
Segmentation fault (core dumped)
Add "-debug" to the ./srcds_run command line to generate a debug.log to help with solving this problem
Thu Dec 17 22:04:45 UTC 2020: Server restart in 10 seconds
import ctypes
from memory.hooks import set_hooks_disabled
from filters.players import PlayerIter

def drop_weapon_test(player, weapon):
    functype = ctypes.CFUNCTYPE(None, *(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p))
    drop_weapon = functype(player.drop_weapon.address)
    player_p = ctypes.c_void_p(player.pointer.address)
    weapon_p = ctypes.c_void_p(weapon.pointer.address)
    set_hooks_disabled(True)
    print("2.1 Drop the weapon by ctypes.")
    drop_weapon(player_p, weapon_p, None, None)
    print("2.2 Success.")
    set_hooks_disabled(False)

    player.equip_weapon(weapon)
    set_hooks_disabled(True)
    print("2.3 Drop the weapon.")
    player.drop_weapon(player, weapon)
    print("2.4 Success.")
    set_hooks_disabled(False)

for player in PlayerIter():
    print('1.1 Trying to remove weapons from {}..'.format(player.name))

    for weapon in player.weapons():
        print('1.1 Found {} from {}. Lets drop it.'.format(weapon.classname, player.name))

        drop_weapon_test(player, weapon)

Server crashed :/

CookStar commented 3 years ago

I should have disabled the hook in equip_weapon as well... my bad.

How about this code?

Code:

def drop_weapon_test(player, weapon):
    set_hooks_disabled(True)
    print("2.1 Drop the weapon.")
    player.drop_weapon(player, weapon)
    print("2.2 Success.")
    set_hooks_disabled(False)
Frag1337 commented 3 years ago

How about this code?

sp plugin load test
[SP] Loading plugin 'test'...
1.1 Trying to remove weapons from Frag..
1.1 Found weapon_knife from Frag. Lets drop it.
2.1 Drop the weapon.
Segmentation fault (core dumped)
Add "-debug" to the ./srcds_run command line to generate a debug.log to help with solving this problem
Thu Dec 17 22:32:55 UTC 2020: Server restart in 10 seconds

test.py

import ctypes
from memory.hooks import set_hooks_disabled
from filters.players import PlayerIter

def drop_weapon_test(player, weapon):
    set_hooks_disabled(True)
    print("2.1 Drop the weapon.")
    player.drop_weapon(player, weapon)
    print("2.2 Success.")
    set_hooks_disabled(False)

for player in PlayerIter():
    print('1.1 Trying to remove weapons from {}..'.format(player.name))

    for weapon in player.weapons():
        print('1.1 Found {} from {}. Lets drop it.'.format(weapon.classname, player.name))

        drop_weapon_test(player, weapon)

Still crashes.

CookStar commented 3 years ago

Still crashes.

Thanks for the test.

Yes, this seems to be an error caused by libffi. Other users are experiencing the same issue on Ubuntu 20.04.

The problem is that I don't know how to solve it. You can use ctypes to work around it, but that's not a solution at all.

I'll do some testing on Ubuntu.

Frag1337 commented 3 years ago

Thanks for trying to help me, very appreciated!

It's so frustrating to have a perfect working plugin on a local windows srcds, but the moment you want to run them publicly on a linux machine, some features which usually worked before doesnt anymore.. Should I reinstall and downgrade to Ubuntu 18, should I switch to Debian? I just dont know because sadly I'm not experienced enough to solve these crashes by myself. Kind of out of my hands.

CookStar commented 3 years ago

Yes, this seems to be an error caused by libffi.

My guess of libffi was wrong. I've added a fix to the problem. #375

Frag1337 commented 3 years ago

Thank you so much for investigating this issue! Very appreciated. I hope it gets pushed very soon!

It would be awesome from you if you could checkout my other issues related to linux.. 👍

Edit: Sorry I havent seen you mentioned my issues in your pull request. You just made my day.