Repository navigation
ports: Raise an error at a negative argument of os.urandom(). - #18833
Conversation
|
Code size report: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #18833 +/- ##
==========================================
- Coverage 98.42% 98.42% -0.01%
==========================================
Files 174 174
Lines 22334 22328 -6
==========================================
- Hits 21983 21977 -6
Misses 351 351 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| vstr_init_len(&vstr, n); | ||
|
|
||
| trng_start(); |
There was a problem hiding this comment.
Why was this line removed?
There was a problem hiding this comment.
Because trng_start() is called again in trng_random_data(). So it was obsolete.
There was a problem hiding this comment.
I see, but it seems like an unrelated change to the subject of the commit
There was a problem hiding this comment.
It is. Just a windfall benefit.
|
@robert-hh , # 3.11.9
>>> os.urandom(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: negative argument not allowed |
|
I had considered both just having |
|
By the way: I did not change the Windows port because I could not spot the source files. The ports/windows directory does not have many of these. I used to build Windows versions > 7 years ago, and it seems I lost track for that port. |
|
It does have the same issue in 1.27 , but afaikt it uses the code from the Unix port for this, so your fix should work. |
I think we should do that first. All existing port-specific implementations look like this: static mp_obj_t mp_os_urandom(mp_obj_t num) {
mp_int_t n = mp_obj_get_int(num);
vstr_t vstr;
vstr_init_len(&vstr, n);
// port specific code to fill vstr.buf
return mp_obj_new_bytes_from_vstr(&vstr);
}
static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom);so could factor that code into static mp_obj_t mp_os_urandom(mp_obj_t num) {
mp_int_t n = mp_obj_get_int(num);
vstr_t vstr;
vstr_init_len(&vstr, n);
mp_os_urandom_bytes(n, vstr.buf);
return mp_obj_new_bytes_from_vstr(&vstr);
}
static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom);and then each port defines @robert-hh would you like to do that? |
If we do this then we can also probably refactor the other places in each port which fill a buffer with random bytes (i.e. similar to the changes in #18834) to call that single helper function, maybe save a little more code size. |
Yes, good idea. In that case maybe copy what the unix port already has: |
@dpgeorge Sure. I had already prepared it, will call that function providing the raw values |
|
@dpgeorge I hav a question about an implementation detail, before changing 11 ports. The unix port defines |
Yes, change the unix port to |
ff8b597 to
bdea95a
Compare
|
@dpgeorge os.orandom() is now factored out. It ended up with quite a few changed files, but the changes are similar for all ports. The actual random numbers are provided by mp_hal_get_random() of each port. The respective implementation for the ports is moved to mphalport.x or equivalent, except for MIMXRT and SAMD, because theses have more than on instance of mp_hal_get_random() calling another function. That is better kept in a single place. The PR still has two commits for visibility during the review, but the first one should be squashed. Using |
5f4c8e0 to
e7c646b
Compare
dpgeorge
left a comment
There was a problem hiding this comment.
It would be good if the two commits could be swapped around: first commit does the refactoring, second commit adds the <0 check.
595fc21 to
52e9f8a
Compare
|
The suggested changes are made. I spotted at least one typical place to use mp_hal_get_ramdom(), which is the function mbedtls_hardware_poll() in mbedtls_port.c. It is changed already for MIMXRT, which used the random number generation from modos already before. There are four more ports where this could be used. Maybe this function can then be moved to another place/file, when it gets port-agnostic. |
Yes, that sounds good. Let's leave it for a follow up PR. |
|
I added a test for |
|
Thanks for adding the test. I was wondering whether a .exp file is missing. But it seems that the approach has changed. |
The approach is still the same. In this case the behaviour matches CPython, so the exp file is generated dynamically as the test is run, via CPython. |
d1ff9e6 to
72fb163
Compare
Factor out mp_os_urandom() of each port into extmod/modos.c, which then calls the port-specific function mp_hal_get_random(). Move mp_hal_get_random() to mphalport where suitable. At the MIMXRT and SAMD it is left in modos.c, since there are different implementation depending on the MCU family. At the ALIF, ESP32, CC3200 and RP2 port the file modos.c was removed, since it was empty after moving mp_hal_get_random(). Tested for the cc3200, esp32, esp8266, mimxrt, nrf, rp2, samd, stm32 and unix ports. Compiled for the alif and the renesas port. Signed-off-by: robert-hh <[email protected]>
Fixes issue micropython#18825. Signed-off-by: robert-hh <[email protected]>
Signed-off-by: Damien George <[email protected]>
72fb163 to
6dbabc9
Compare
dpgeorge
left a comment
There was a problem hiding this comment.
This looks good, it's a really nice clean up / consolidation across the ports.
|
Thank you very much. |
|
Thanks @robert-hh! This is indeed a very nice consolidation to have, much appreciated. |
Summary
Raise an error at a negative argument of os.urandom().
Testing
Tested for the cc3200, esp32, esp8266, mimxrt, nrf, rp2, samd, stm32 and unix ports. Compiled for the alif and the renesas port but not tested lacking suitable boards.
Argument values -1, 0, 3
Trade-offs and Alternatives
The PR repeats the same few lines of code for each port. That looks not nice. I tried to factor out the common code part if mp_os_urandom(), but that fails for quite a few build vaiants. To be tested further, if needed.