Skip to content
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

Polyfill dns_check_record(), dns_get_record(), and dns_get_mx() #1067

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented Feb 27, 2024

Closes #1042 by polyfilling the dns_* functions that are missing in the WebAssembly
build of PHP. In the PHP source code, these functions are included
conditionally when the HAVE_DNS_SEARCH_FUNC constant is true, which it
isn't in the Emscripten build.

The polyfills return either false or an empty array and do not
actually perform any DNS checks.

In Node.js, we could support actual dns checks. Let's leave that for a
subsequent iteration. In the browser, there isn't much we can do.

Remaining work

Rebuild all the PHP versions or else the CI will fail

Testing instructions

Confirm the CI checks pass

CC @p-jackson @sejas @wojtekn @artpi

Closes #1042 by
polyfilling the dns_* functions that are missing in the WebAssembly
build of PHP. In the PHP source code, these functions are included
conditionally when the HAVE_DNS_SEARCH_FUNC constant is true, which it
isn't in the Emscripten build.

The polyfills return either `false` or an empty array and do not
actually perform any DNS checks.

In Node.js, we could support actual dns checks. Let's leave that for a
subsequent iteration. In the browser, there isn't much we can do.

 ## Remaining work

Rebuild the browser versions of PHP

 ## Testing instructions

Confirm the CI checks pass

CC @p-jackson @sejas @wojtekn
Copy link
Member

@p-jackson p-jackson left a comment

Choose a reason for hiding this comment

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

Great to have a fix built right into php-wasm 😊

I'm just pondering here, I could be wrong, but are these polyfills almost too graceful? dns_get_record( 'google.com' ) will return an empty array and the calling code has no idea nothing happened. We could return false to indicate an error, but I'm worried that might break plugins that aren't handling errors correctly (they never needed to anticipate a situation where google.com has no DNS records).

Would logging a warning be appropriate? Something like WordPress's _doing_it_wrong() function. If a plugin isn't working in Playground, a log would give the plugin author a hint to go on.

packages/php-wasm/compile/php/php_wasm.c Outdated Show resolved Hide resolved
packages/php-wasm/compile/php/php_wasm.c Show resolved Hide resolved
packages/php-wasm/compile/php/php_wasm.c Show resolved Hide resolved
packages/php-wasm/node/src/test/php.spec.ts Show resolved Hide resolved
@adamziel
Copy link
Collaborator Author

Would logging a warning be appropriate? Something like WordPress's _doing_it_wrong() function. If a plugin isn't working in Playground, a log would give the plugin author a hint to go on.

Good thinking! I added a PHP warning to those functions, I'll just need to adjust the tests to expect that warning.

@adamziel
Copy link
Collaborator Author

adamziel commented Mar 4, 2024

@p-jackson I may need to sidetrack the remaining bit of the work in this PR. Would you be up for taking over so we could get it in?

@p-jackson
Copy link
Member

I'll see what I can do 😅🤞
Can't guarantee when I'll get to it.

@bgrgicak
Copy link
Collaborator

Almost done. I just need to find the right place to add DNS constants so they are always available, but not duplicated.

@bgrgicak bgrgicak requested a review from a team as a code owner May 23, 2024 06:51
@bgrgicak bgrgicak self-assigned this May 23, 2024
@bgrgicak bgrgicak requested a review from p-jackson May 23, 2024 07:10
@bgrgicak
Copy link
Collaborator

@adamziel this should now be ready for a review

Copy link
Member

@p-jackson p-jackson left a comment

Choose a reason for hiding this comment

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

Thanks for adding the messages 👍 I think it's much kinder to devs than pretending everything worked fine 😊

I tested it manually using this little script

const { NodePHP } = require('./dist/packages/php-wasm/node');

NodePHP.load('8.0')
   .then((php) => php.run({ code: '<?php dns_get_record("google.com");'}) )
   .then((output) => console.log(output.text));

I noticed that the message is incorrect if you call an alias e.g. when calling getmxrr() the warning refers to dns_get_mx. But I think that's fine, just wanted to note it here.

packages/php-wasm/compile/php/dns_polyfill.c Outdated Show resolved Hide resolved
@@ -25,6 +25,7 @@
#include "rfc1867.h"
#include "SAPI.h"
#include "proc_open.h"
#include "dns_polyfill.c"
Copy link
Member

Choose a reason for hiding this comment

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

#includeing a *.c file seems a bit unorthodox. Doesn't this leave us open to having issues with double definitions down the track?

I feel it would be more typical to ensure dns_polyfill.c gets compiled separately and then linked into the final output? i.e. including it in the build command here:

We'd need another header file to declare the functions so they can be included in the additional_functions array. But still, that seems a bit more conventional.

But I might be missing something. Was there a reason why this needed to be included as a C file? Could the DNS functions simply be added to the giant php_wasm.c file?

Co-authored-by: Philip Jackson <p-jackson@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Core PHP function dns_get_record is undefined
3 participants