PHP questions here :)

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

almond
Posts: 4
Joined: Mon Oct 19, 2009 11:34 pm UTC

PHP questions here :)

Postby almond » Tue Oct 20, 2009 1:53 am UTC

I'm occasionally bored at work and head over here. So please, if you have a PHP question, I will be glad to answer them or point you in the right direction :).

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: PHP questions here :)

Postby You, sir, name? » Tue Oct 20, 2009 2:14 am UTC

almond wrote:I'm occasionally bored at work and head over here. So please, if you have a PHP question, I will be glad to answer them or point you in the right direction :).


How do I do inline assembly in PHP?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
OOPMan
Posts: 314
Joined: Mon Oct 15, 2007 10:20 am UTC
Location: Cape Town, South Africa

Re: PHP questions here :)

Postby OOPMan » Tue Oct 20, 2009 7:51 am UTC

You, sir, name? wrote:
almond wrote:I'm occasionally bored at work and head over here. So please, if you have a PHP question, I will be glad to answer them or point you in the right direction :).


How do I do inline assembly in PHP?


Write your own PHP extension that dynamically executes assembly code passed to it in a string?
Image

Image

Two9A
Posts: 194
Joined: Sat Jul 26, 2008 11:22 pm UTC
Location: The smogbound wastes of northern England
Contact:

Re: PHP questions here :)

Postby Two9A » Tue Oct 20, 2009 7:55 am UTC

You know, that's not a half-bad idea. An extension with an inbuilt assembler wouldn't be too tough; the only problem would come in sandboxing the execution. It'd get far too easy to segfault the thing if it was left to roam free over the address lines.

Another question: why are PayPal's recurring payments such an ass to set up? (Perhaps not PHP-specific, but eh.)
The Unofficial "Making xkcd Slightly Worse" Archive [Incomplete]: xkcdsw.com
Articles that fall out of my head about once a month: imrannazar.com

User avatar
OOPMan
Posts: 314
Joined: Mon Oct 15, 2007 10:20 am UTC
Location: Cape Town, South Africa

Re: PHP questions here :)

Postby OOPMan » Tue Oct 20, 2009 10:16 am UTC

Two9A wrote:You know, that's not a half-bad idea. An extension with an inbuilt assembler wouldn't be too tough; the only problem would come in sandboxing the execution. It'd get far too easy to segfault the thing if it was left to roam free over the address lines.


No, it's not half-bad, it's whole-bad.
Image

Image

Unparallelogram
Posts: 336
Joined: Mon Jul 28, 2008 4:16 am UTC

Re: PHP questions here :)

Postby Unparallelogram » Wed Oct 21, 2009 12:21 am UTC

How do you recommend structuring a large-ish project made from php+mysql+html+js? In particular, I want to make a browser game that integrates as a facebook app. Thanks in advance.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: PHP questions here :)

Postby You, sir, name? » Wed Oct 21, 2009 12:24 am UTC

OOPMan wrote:
You, sir, name? wrote:
almond wrote:I'm occasionally bored at work and head over here. So please, if you have a PHP question, I will be glad to answer them or point you in the right direction :).


How do I do inline assembly in PHP?


Write your own PHP extension that dynamically executes assembly code passed to it in a string?


Next PHP question: How do I do that?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
psykx
Posts: 408
Joined: Sat Feb 23, 2008 11:24 pm UTC
Location: England
Contact:

Re: PHP questions here :)

Postby psykx » Wed Oct 21, 2009 12:40 am UTC

Unparallelogram wrote:How do you recommend structuring a large-ish project made from php+mysql+html+js? In particular, I want to make a browser game that integrates as a facebook app. Thanks in advance.


MVC
Berengal wrote:Only if they're killer robots. Legos are happy robots. Besides, even if they were killer robots it wouldn't stop me. You can't stop science and all that.

Unparallelogram
Posts: 336
Joined: Mon Jul 28, 2008 4:16 am UTC

Re: PHP questions here :)

Postby Unparallelogram » Wed Oct 21, 2009 12:52 am UTC

psykx wrote:
Unparallelogram wrote:How do you recommend structuring a large-ish project made from php+mysql+html+js? In particular, I want to make a browser game that integrates as a facebook app. Thanks in advance.


MVC

I always thought MVC was a bad paradigm for games in particular, cause they were more oriented towards modeling business-y data.

User avatar
'; DROP DATABASE;--
Posts: 3284
Joined: Thu Nov 22, 2007 9:38 am UTC
Location: Midwest Alberta, where it's STILL snowy
Contact:

Re: PHP questions here :)

Postby '; DROP DATABASE;-- » Wed Oct 21, 2009 5:24 am UTC

You, sir, name? wrote:
OOPMan wrote:
You, sir, name? wrote:
almond wrote:I'm occasionally bored at work and head over here. So please, if you have a PHP question, I will be glad to answer them or point you in the right direction :).


How do I do inline assembly in PHP?


Write your own PHP extension that dynamically executes assembly code passed to it in a string?


Next PHP question: How do I do that?
Assembly would be a little tricky; you'd need to assemble it (which realistically probably means calling "as" to do it for you :P). Machine code would probably be a simple matter of something like:
int some_callback_from_php(char *code)
{
(void*)code();
}

I'm not sure how you'd make such a ridiculous cast. >_>

I'd be rather amused to see what kind of ridiculous abuses of PHP newbies do with that capability. :P
poxic wrote:You suck. And simultaneously rock. I think you've invented a new state of being.

bieber
Posts: 223
Joined: Thu Mar 13, 2008 12:13 am UTC

Re: PHP questions here :)

Postby bieber » Wed Oct 21, 2009 6:06 am UTC

I can only hope the inline assembler in PHP never happens. This would just bring a whole new dimension of terrifying to SQL injection :P

User avatar
'; DROP DATABASE;--
Posts: 3284
Joined: Thu Nov 22, 2007 9:38 am UTC
Location: Midwest Alberta, where it's STILL snowy
Contact:

Re: PHP questions here :)

Postby '; DROP DATABASE;-- » Wed Oct 21, 2009 6:12 am UTC

Machine code injection is always fun. Like a buffer overflow but stupider!

The real interesting part though would be seeing who's dumb enough to leave such a gaping security hole, yet capable of using machine code in a PHP script in the first place. Hmm.
poxic wrote:You suck. And simultaneously rock. I think you've invented a new state of being.

User avatar
phlip
Restorer of Worlds
Posts: 7556
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: PHP questions here :)

Postby phlip » Wed Oct 21, 2009 6:53 am UTC

'; DROP DATABASE;-- wrote:I'm not sure how you'd make such a ridiculous cast. >_>

Code: Select all

void run_machine_code(void *code)
{
   if (cpu_supports_DEP())
      do_something(clever);
   ((void (*)(void))code)(); // Please forgive me, language gods...
}

I'm not sure exactly how you get around DEP, but it must be doable, JIT interpreters manage it after all... there's probably some platform-specific call to do it, since DEP is meant to protect against accidental jumps to data, not deliberate ones... it wouldn't be portable, but then it's not portable anyway (possibly not even between compilers on the same platform, if they have different calling conventions, or they refuse to compile that illegal-according-to-the-spec cast... but certainly not between platforms with different instruction sets).

It works for me, in any rate:

Code: Select all

phlip@boris:/tmp$ cat machinecode.c
#include <stdio.h>

/*
MOV EAX, 42
RETN
*/
const void *sample_code = "\xB8\x2A\x00\x00\x00\xC3";

int main(void)
{
   int result = ((int __attribute__ ((cdecl)) (*)(void))sample_code)(); // This line is its own punishment...
   printf("Code returns: %d\n", result);
   return 0;
}

phlip@boris:/tmp$ gcc -Wall -W -pedantic machinecode.c -o machinecode
tmp.c: In function `main':
tmp.c:12: warning: ISO C forbids conversion of object pointer to function pointer type

phlip@boris:/tmp$ ./machinecode
Code returns: 42


[ninja-edit]
'; DROP DATABASE;-- wrote:Machine code injection is always fun. Like a buffer overflow but stupider!

Exactly! People are all the time saying "Turing-equivalence means that any program you can write in one language you can write in another" and "This language protects you from having buffer overflows or data execution" and I'm all "make up your minds, people!" Why would I want to use a language that would limit the programs you can write?

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: PHP questions here :)

Postby You, sir, name? » Wed Oct 21, 2009 1:25 pm UTC

I don't like directly casting to a function pointer. Feels like this step makes it all the more clearer:

Code: Select all

int some_callback_from_php(char *code)
{
    void (*fp)() = (void *) code;
    fp();
}


But you know, ... whatever.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

almond
Posts: 4
Joined: Mon Oct 19, 2009 11:34 pm UTC

Re: PHP questions here :)

Postby almond » Wed Oct 21, 2009 7:24 pm UTC

Two9A wrote:You know, that's not a half-bad idea. An extension with an inbuilt assembler wouldn't be too tough; the only problem would come in sandboxing the execution. It'd get far too easy to segfault the thing if it was left to roam free over the address lines.

Another question: why are PayPal's recurring payments such an ass to set up? (Perhaps not PHP-specific, but eh.)


Funny you ask, I've been looking into the same thing and you know the reason already ;). Security issues.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 2 guests