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

include/uk/plat/memory.h: Add check for memory region descriptor #797

Closed
wants to merge 1 commit into from

Conversation

Daparrag
Copy link
Contributor

Prerequisite checklist

  • Read the contribution guidelines regarding submitting new changes to the project;
  • Tested your changes against relevant architectures and platforms;
  • Ran the checkpatch.pl on your commit series before opening this PR;
  • Updated relevant documentation.

Base target

  • Architecture(s): x86_64
  • Platform(s): linuxu
  • Application(s): [e.g. helloword or N/A]

Additional configuration

none

Description of changes

according to #766 the helloworld example was broken because an invalid memory allocator.

[443184.280410] Info: [libukboot] <boot.c @  245> Unikraft constructor table at 0x40e000 - 0x40e008
[443184.280459] dbg:  [libukboot] <boot.c @  249> Call constructor: 0x40d400())...
[443184.280509] dbg:  [libcontext] <ectx.c @   72> Load/store of extended CPU state: XSAVEOPT
[443184.280547] Info: [libukboot] <boot.c @  264> Initialize memory allocator...
[443184.280570] CRIT: [libukboot] <boot.c @  268> Failed to initialize memory allocator
[Inferior 1 (process 34651) exited with code 01]

This problem occurred because you attempted to access a page without first determining the total number of available pages. To clarify further, the function ukplat_memregion_find_next requires the count of memory regions to be validated before calling the ukplat_memregion_find_next function to obtain the desired memory region.

For this reason I add a sanity check count = ukplat_memregion_count(); that will avoid the program to advance if the memory region is greater or equal than memory region count.

With this changes the hello world works as expected

---output after fixing

 ./helloworld_linuxu-x86_64
[447357.835182] dbg:  [libukboot] <boot.c @  249> Call constructor: 0x40c1f0())...
[447357.835227] dbg:  [libcontext] <ectx.c @   72> Load/store of extended CPU state: XSAVEOPT
[447357.835281] dbg:  [liblinuxuplat] <memory.c @   84> No initrd present.
[447357.835321] dbg:  [libukboot] <boot.c @  189> Trying 0x7f1c14674000-0x7f1c14a74000 0x00
[447357.835401] dbg:  [libukallocbbuddy] <bbuddy.c @  471> 7f1c14674000: Add allocate unit 7f1c14676000 - 7f1c14678000 (order 1)
[447357.835440] dbg:  [libukallocbbuddy] <bbuddy.c @  471> 7f1c14674000: Add allocate unit 7f1c14678000 - 7f1c14680000 (order 3)
[447357.835508] dbg:  [libukallocbbuddy] <bbuddy.c @  471> 7f1c14674000: Add allocate unit 7f1c14680000 - 7f1c14700000 (order 7)
[447357.835590] dbg:  [libukallocbbuddy] <bbuddy.c @  471> 7f1c14674000: Add allocate unit 7f1c14700000 - 7f1c14800000 (order 8)
[447357.835687] dbg:  [libukallocbbuddy] <bbuddy.c @  471> 7f1c14674000: Add allocate unit 7f1c14800000 - 7f1c14a00000 (order 9)
[447357.835810] dbg:  [libukallocbbuddy] <bbuddy.c @  471> 7f1c14674000: Add allocate unit 7f1c14a00000 - 7f1c14a40000 (order 6)
[447357.835865] dbg:  [libukallocbbuddy] <bbuddy.c @  471> 7f1c14674000: Add allocate unit 7f1c14a40000 - 7f1c14a60000 (order 5)
[447357.835920] dbg:  [libukallocbbuddy] <bbuddy.c @  471> 7f1c14674000: Add allocate unit 7f1c14a60000 - 7f1c14a70000 (order 4)
[447357.835995] dbg:  [libukallocbbuddy] <bbuddy.c @  471> 7f1c14674000: Add allocate unit 7f1c14a70000 - 7f1c14a74000 (order 2)
[447357.836074] dbg:  [libcontext] <tls.c @  173> tls_area_init: target: 0x7f1c14676020 (16 bytes)
[447357.836110] dbg:  [libcontext] <tls.c @  175> tls_area_init: copy (.tdata): 0 bytes
[447357.836163] dbg:  [libcontext] <tls.c @  177> tls_area_init: uninitialized (.tbss): 4 bytes
[447357.836203] dbg:  [libcontext] <tls.c @  179> tls_area_init: pad: 4 bytes
[447357.836257] dbg:  [libcontext] <tls.c @  181> tls_area_init: tcb: 8 bytes
[447357.836327] dbg:  [libcontext] <tls.c @  183> tls_area_init: tcb self ptr: 0x7f1c14676028
[447357.836370] dbg:  [libcontext] <tls.c @  204> (tls_area): 7f1c14676020  00 00 00 00 00 00 00 00  28 60 67 14 1c 7f 00 00  |........(`g.....|
[447357.836642] dbg:  [libuksched] <thread.c @  254> uk_thread 0x7f1c146770b0 (idle): ctx:0x7f1c146770b0, ectx:0x7f1c14a70040, tlsp:0x7f1c14a70028
[447357.836678] dbg:  [libcontext] <tls.c @  173> tls_area_init: target: 0x7f1c14a70020 (16 bytes)
[447357.836727] dbg:  [libcontext] <tls.c @  175> tls_area_init: copy (.tdata): 0 bytes
[447357.836775] dbg:  [libcontext] <tls.c @  177> tls_area_init: uninitialized (.tbss): 4 bytes
[447357.836818] dbg:  [libcontext] <tls.c @  179> tls_area_init: pad: 4 bytes
[447357.836840] dbg:  [libcontext] <tls.c @  181> tls_area_init: tcb: 8 bytes
[447357.836862] dbg:  [libcontext] <tls.c @  183> tls_area_init: tcb self ptr: 0x7f1c14a70028
[447357.836887] dbg:  [libcontext] <tls.c @  204> (tls_area): 7f1c14a70020  00 00 00 00 00 00 00 00  28 00 a7 14 1c 7f 00 00  |........(.......|
[447357.837259] dbg:  [libcontext] <ctx.c @  133> ukarch_ctx 0x7f1c146770b0: entry:0x40af80(7f1c14677018), sp:0x7f1c14a50018
[447357.837308] dbg:  [libuksched] <thread.c @  254> uk_thread 0x7f1c14a71018 (init): ctx:0x7f1c14a71018, ectx:0x7f1c14a71100, tlsp:0x7f1c14676028
Powered by
o.   .o       _ _               __ _
Oo   Oo  ___ (_) | __ __  __ _ ' _) :_
oO   oO ' _ `| | |/ /  _)' _` | |_|  _)
oOo oOO| | | | |   (| | | (_) |  _) :_
 OoOoO ._, ._:_:_,\_._,  .__,_:_, \___)
      Epimetheus 0.12.0~4c7352c0-custom
Hello world!
Arguments:  "./helloworld_linuxu-x86_64"

      _
    c'_'o  .--'
    (| |)_/            [447357.837418] dbg:  [libposix_time] <time.c @   65> (in      _                ep((const struct timespec*) 0x7ffd00121bc0, (struct times    c'o'o  .--.
    (| |)_/            [447358.087798] dbg:  [libposix_time] <time.c @   65> (in      _                ep((const struct timespec*) 0x7ffd00121bc0, (struct times    c'_'o  .-.
    (| |)_/   `        [447358.338165] dbg:  [libposix_time] <time.c @   65> (in      _                ep((const struct timespec*) 0x7ffd00121bc0, (struct times

#766

Make sure that the value of the memory region descriptor index is not
bigger than the total count of memory region descriptors.

Signed-off-by: guzman <daparrag@correo.udistrital.edu.co>
@Daparrag Daparrag requested review from a team as code owners March 11, 2023 18:19
@razvand razvand self-assigned this Mar 11, 2023
@razvand razvand requested review from mogasergiu and RaduNichita and removed request for a team March 11, 2023 19:10
@razvand razvand added the plat/linuxu Unikraft for Linux Userspace label Mar 11, 2023
@razvand razvand added this to the v0.13.0 (Atlas) milestone Mar 11, 2023
@razvand razvand added the topic/mm Topics pertaining to memory management label Mar 11, 2023
@razvand
Copy link
Contributor

razvand commented Mar 11, 2023

@RaduNichita, @mogasergiu, please review this PR. It supersedes PR #771.

@razvand
Copy link
Contributor

razvand commented Mar 11, 2023

@Daparrag, in the future, simply update the commits in the open PR by using git force --push. You don't have to close it and open a new one.

@razvand razvand linked an issue Mar 16, 2023 that may be closed by this pull request
Copy link
Contributor

@razvand razvand left a comment

Choose a reason for hiding this comment

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

Thanks, this works.

See my question about the comma. And please make sure you use the same name and e-mail (ideally your full name) as part of the commit message.

Also mention the issue as part of the commit message, not just the solution.

@@ -190,7 +190,7 @@ static struct uk_alloc *heap_init()
(void *)md->vbase, (void *)(md->vbase + md->len),
md->flags,
#if CONFIG_UKPLAT_MEMRNAME
md->name
md->name,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you add a comma here?

@@ -156,6 +161,9 @@ int ukplat_memregion_get(int i, struct ukplat_memregion_desc **m)
int ret;

UK_ASSERT(m);
Copy link
Member

Choose a reason for hiding this comment

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

Add a newline between the UK_ASSERT and the if statement, I think it would look better.

@@ -127,10 +127,13 @@ static int __linuxu_plat_initrd_init(void)

int ukplat_memregion_count(void)
{
static int init = 0;
static int have_heap = 0;
static int have_initrd = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Also, I believe that my example from the previously closed PR dropped the static keyword from have_heap and have_initrd. Why do you think they are still needed? As far as I can tell, static for init would be enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we keep these variables? I think it would be cleaner if we use only the rc and remove both have_heap and have_initrd

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, agreed, you are right. Something like this

int ukplat_memregion_count(void)
{
	static int init;

	if (init)
		return init;
		
	init = !__linuxu_plat_heap_init() + !__linuxu_plat_initrd_init();

	return init;
}

could work as well and you can drop everything but init :neckbeard:.
Or

int ukplat_memregion_count(void)
{
	static int init;
	int rc;

	if (init)
		return init;
	
	rc = __linuxu_plat_heap_init();
	init += (rc == 0) ? 1 : 0 ;

	rc = __linuxu_plat_initrd_init();
	init += (rc == 0) ? 1 : 0 ;
	
	return init;
}

and the list could go on 😆 .
I don't think it matters that much in the end, though. @Daparrag choose whichever you like, as long as it works, looks decent and does not have any unnecessary statements (I would personally go with the latter).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good Thank you so much for your review and I will start working on that.

@@ -127,10 +127,13 @@ static int __linuxu_plat_initrd_init(void)

int ukplat_memregion_count(void)
Copy link
Member

Choose a reason for hiding this comment

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

Oh and one more thing. Based on your commit description, the change to ukplat_memregion_count should be in a separate commit, as it does not fix anything, but rather just adds a minor improvement. So, I would suggest you place this in a commit with a message like Avoid re-init of heap and initrd in ukplat_memregion_count or something like that and the actual fix with your current message in a separate commit. This way, this PR should have 2 commits instead of one: a commit with your current message and the changes done to ukplat_memregion_get (your actual fix) and a commit with your changes to ukplat_memregion_count (the improvement). This is how I would see it, just my opinion ;).

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @mogasergiu

@@ -127,10 +127,13 @@ static int __linuxu_plat_initrd_init(void)

int ukplat_memregion_count(void)
{
static int init = 0;
static int have_heap = 0;
static int have_initrd = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we keep these variables? I think it would be cleaner if we use only the rc and remove both have_heap and have_initrd

@@ -127,10 +127,13 @@ static int __linuxu_plat_initrd_init(void)

int ukplat_memregion_count(void)
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @mogasergiu

@razvand
Copy link
Contributor

razvand commented Apr 10, 2023

@Daparrag, please see the reviews, update the PR, so we can then merge it.

@razvand
Copy link
Contributor

razvand commented May 8, 2023

Solved by #878

@razvand razvand closed this May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plat/linuxu Unikraft for Linux Userspace topic/mm Topics pertaining to memory management
Projects
Status: Done!
Development

Successfully merging this pull request may close these issues.

lib/ukboot: Critical error when running app-helloworld on linuxu
4 participants